Skip to content

Commit

Permalink
Merge pull request #11 from byerlikaya/refactor-and-package-update
Browse files Browse the repository at this point in the history
Refactor and package update
  • Loading branch information
byerlikaya authored Apr 14, 2024
2 parents 99bb2ee + 6b863db commit d4d64eb
Show file tree
Hide file tree
Showing 24 changed files with 670 additions and 698 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Baris Yerlikaya
Copyright (c) 2024 Baris Yerlikaya

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 16 additions & 17 deletions sample/Sample.Api/ApplicationSpecific/Contexts/MemoryDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
using Sample.Common.Entity;
using System.Reflection;

namespace Sample.Api.ApplicationSpecific.Contexts
namespace Sample.Api.ApplicationSpecific.Contexts;

public class MemoryDbContext : DbContext
{
public class MemoryDbContext : DbContext
{
public DbSet<Publisher> Publishers { get; set; }
public DbSet<Publisher> Publishers { get; set; }

public DbSet<Book> Books { get; set; }
public DbSet<Book> Books { get; set; }

public DbSet<Author> Authors { get; set; }
public DbSet<Author> Authors { get; set; }

public DbSet<Country> Countries { get; set; }
public DbSet<Country> Countries { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
if (!optionsBuilder.IsConfigured)
{
base.OnConfiguring(optionsBuilder.UseInMemoryDatabase("DummyDataDb"));
}
base.OnConfiguring(optionsBuilder.UseInMemoryDatabase("DummyDataDb"));
}
}
}
}
19 changes: 9 additions & 10 deletions sample/Sample.Api/ApplicationSpecific/PublisherRepository.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Sample.Api.ApplicationSpecific.Contexts;
using Sample.Common.Entity;

namespace Sample.Api.ApplicationSpecific
namespace Sample.Api.ApplicationSpecific;

public class PublisherRepository
{
public class PublisherRepository
{
private readonly MemoryDbContext _context = new();
private readonly MemoryDbContext _context = new();

public void AddRange(IEnumerable<Publisher> publishers)
{
_context.AddRange(publishers);
_context.SaveChanges();
}
public void AddRange(IEnumerable<Publisher> publishers)
{
_context.AddRange(publishers);
_context.SaveChanges();
}
}
}
56 changes: 24 additions & 32 deletions sample/Sample.Api/Controllers/PublisherController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,33 @@
using Sample.Common.Entity;
using SmartWhere;

namespace Sample.Api.Controllers
namespace Sample.Api.Controllers;

[ApiController]
public class PublisherController(MemoryDbContext context) : ControllerBase
{
[ApiController]
public class PublisherController : ControllerBase
[HttpPost("/publishers")]
public IActionResult GetPublishers(PublisherSearchRequest request)
{
private readonly MemoryDbContext _context;

public PublisherController(MemoryDbContext context)
{
_context = context;
}

[HttpPost("/publishers")]
public IActionResult GetPublishers(PublisherSearchRequest request)
{
var result = _context.Set<Publisher>()
.Include(x => x.Books)
.ThenInclude(x => x.Author)
.Where(request)
.ToList();
var result = context.Set<Publisher>()
.Include(x => x.Books)
.ThenInclude(x => x.Author)
.Where(request)
.ToList();

return Ok(result);
}
return Ok(result);
}

[HttpPost("/books")]
public IActionResult GetBooks(BookSearchRequest request)
{
var result = _context.Books
.Include(x => x.Author)
.Where(request)
.Skip(request.Start)
.Take(request.Max)
.ToList();
[HttpPost("/books")]
public IActionResult GetBooks(BookSearchRequest request)
{
var result = context.Books
.Include(x => x.Author)
.Where(request)
.Skip(request.Start)
.Take(request.Max)
.ToList();

return Ok(result);
}
return Ok(result);
}
}
}
17 changes: 8 additions & 9 deletions sample/Sample.Api/PublisherData.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Sample.Common;

namespace Sample.Api
namespace Sample.Api;

public static class PublisherData
{
public static class PublisherData
public static void FillDummyData()
{
public static void FillDummyData()
{
PublisherRepository publisherRepository = new();
var publisher = DataInitializer.FillMockData().ToList();
publisherRepository.AddRange(publisher);
}
PublisherRepository publisherRepository = new();
var publisher = DataInitializer.FillMockData().ToList();
publisherRepository.AddRange(publisher);
}
}
}
19 changes: 9 additions & 10 deletions sample/Sample.Api/PublisherRepository.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Sample.Api.ApplicationSpecific.Contexts;
using Sample.Common.Entity;

namespace Sample.Api
namespace Sample.Api;

public class PublisherRepository
{
public class PublisherRepository
{
private readonly MemoryDbContext _context = new();
private readonly MemoryDbContext _context = new();

public void AddRange(IEnumerable<Publisher> publishers)
{
_context.AddRange(publishers);
_context.SaveChanges();
}
public void AddRange(IEnumerable<Publisher> publishers)
{
_context.AddRange(publishers);
_context.SaveChanges();
}
}
}
8 changes: 4 additions & 4 deletions sample/Sample.Api/Sample.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
Loading

0 comments on commit d4d64eb

Please sign in to comment.