Skip to content

Commit

Permalink
feat: added entities' configuration for domain model
Browse files Browse the repository at this point in the history
  • Loading branch information
sashafromlibertalia committed Jun 13, 2023
1 parent 093a401 commit 75ffe04
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Animelib.Backend/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ dotnet_diagnostic.CA1419.severity = warning
dotnet_diagnostic.CA1501.severity = warning
dotnet_diagnostic.CA1502.severity = warning
dotnet_diagnostic.CA1505.severity = warning
dotnet_diagnostic.CA1506.severity = warning
dotnet_diagnostic.CA1506.severity = none
dotnet_diagnostic.CA1507.severity = suggestion
dotnet_diagnostic.CA1508.severity = warning
# dotnet_diagnostic.CA1509.severity = none
Expand Down Expand Up @@ -562,4 +562,4 @@ csharp_style_prefer_utf8_string_literals = true:suggestion
resharper_csharp_empty_block_style = together_same_line

csharp_wrap_arguments_style=chop_if_long
csharp_wrap_chained_method_calls=chop_if_long
csharp_wrap_chained_method_calls=chop_if_long
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Animelib.Core.Users;
using Animelib.Dto.Identity;

namespace Animelib.Services.Abstractions;

public interface IIdentityService
{
Task<User> CreateUserAsync(CreateUserDto dto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
<ProjectReference Include="..\Animelib.Dto\Animelib.Dto.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.15.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.15.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Animelib.Services.Services;
public class AnimeService : IAnimeService
{
private readonly IDatabaseContext _context;

public AnimeService(IDatabaseContext context)
{
_context = context;
Expand All @@ -20,13 +20,13 @@ public AnimeService(IDatabaseContext context)
public Task<AnimeListDto> GetAnimesAsync(int page, int take)
{
var total = _context.Animes.Count();

var animes = _context.Animes
.Skip((page - 1) * take)
.Take(take)
.AsEnumerable()
.Select(x => x.ToDto());

return Task.FromResult(new AnimeListDto(animes, total));
}

Expand All @@ -48,7 +48,7 @@ public async Task<AnimeDto> GetAnimeBySlugAsync(string slug)
{
throw new EntityNotFoundException($"Anime with this slug not found: {slug}");
}

return anime.ToDto();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Animelib.Core.Animes;
using Animelib.Core.Users;
using Animelib.DataAccess.Abstractions;
using Animelib.Dto.Identity;
using Animelib.Services.Abstractions;

namespace Animelib.Services.Services;

public class IdentityService : IIdentityService
{
private readonly IDatabaseContext _context;

public IdentityService(IDatabaseContext context)
{
_context = context;
}

public Task<User> CreateUserAsync(CreateUserDto dto)
{
var user = new User(dto.Username, dto.Email, dto.Password);

user.AddWatchList(new WatchList("Completed", user, new List<Anime>()));
user.AddWatchList(new WatchList("Watching", user, new List<Anime>()));
user.AddWatchList(new WatchList("On Hold", user, new List<Anime>()));
user.AddWatchList(new WatchList("Dropped", user, new List<Anime>()));
user.AddWatchList(new WatchList("Plan to Watch", user, new List<Anime>()));
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Animelib.Core.Animes;
using Animelib.Core.Users;
using Microsoft.EntityFrameworkCore;

namespace Animelib.DataAccess.Abstractions;

public interface IDatabaseContext
{
DbSet<Anime> Animes { get; }
DbSet<User> Users { get; }
DbSet<WatchList> WatchLists { get; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ namespace Animelib.Dto.Animes;
public record AnimeListDto
(
IEnumerable<AnimeDto> Data,
int total
int Total
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Animelib.Dto.Identity;

public record CreateUserDto(string Username, string Email, string Password);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Animelib.Dto.Identity;

public record LoginUserDto(string Email, string Password);
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public AnimeValidator()
{
RuleFor(anime => anime.Id)
.NotEmpty().GreaterThan(0).WithMessage("ID can't be negative or zero");

RuleFor(anime => anime.Description)
.NotNull().NotEmpty().WithMessage("Description can't be null or empty");

RuleFor(anime => anime.Slug)
.NotEmpty().NotNull().WithMessage("Slug can't be null or empty");

RuleFor(anime => anime.Title)
.NotEmpty().NotNull().WithMessage("Title can't be null or empty");

Expand All @@ -27,8 +27,8 @@ public AnimeValidator()

RuleFor(anime => anime.Rating)
.NotEmpty().GreaterThanOrEqualTo(0).WithMessage("Rating can't be a negative number");

RuleFor(anime => anime.Subtype)
.NotEmpty().NotNull().WithMessage("Subtype can't be empty or null");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Animelib.Core.Users;
using FluentValidation;

namespace Animelib.Common.Validators;

public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(user => user.Email)
.NotEmpty()
.NotNull()
.EmailAddress()
.WithMessage("Email can't be null or empty");

RuleFor(user => user.Username)
.NotEmpty()
.NotNull()
.WithMessage("Email can't be null or empty");

RuleFor(user => user.Password)
.Length(6, 20)
.NotEmpty()
.NotNull()
.WithMessage("Password can't be null or empty");
}
}
4 changes: 3 additions & 1 deletion Animelib.Backend/Domain/Animelib.Core/Animes/Anime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RichEntity.Annotations;
using Animelib.Core.Users;
using RichEntity.Annotations;

namespace Animelib.Core.Animes;

Expand Down Expand Up @@ -29,4 +30,5 @@ public Anime(int id, string title, string slug, string description, string? post
public int? EpisodeCount { get; init; }
public float Rating { get; init; }
public string Subtype { get; init; }
public IReadOnlyList<WatchList> WatchLists { get; init; } = new List<WatchList>();
}
25 changes: 18 additions & 7 deletions Animelib.Backend/Domain/Animelib.Core/Users/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ namespace Animelib.Core.Users;

public partial class User : IEntity<int>
{
public User(int id, string username, UserRoleEnum role = UserRoleEnum.Viewer)
private readonly List<WatchList> _watchLists;
public User(string username, string email, string password)
{
Id = id;
Username = username;
Role = role;
Email = email;
Password = password;
_watchLists = new List<WatchList>();
}

public string Username { get; init; }
public string Email { get; init; }
public string Password { get; init; }
public IReadOnlyList<WatchList> WatchLists => _watchLists;

public int Id { get; }
public string Username { get; }
public UserRoleEnum Role { get; }
}
public void AddWatchList(WatchList watchList)
{
if (watchList is null)
throw new NullReferenceException("WatchList cannot be null");

_watchLists.Add(watchList);
}
}
8 changes: 0 additions & 8 deletions Animelib.Backend/Domain/Animelib.Core/Users/UserRoleEnum.cs

This file was deleted.

18 changes: 18 additions & 0 deletions Animelib.Backend/Domain/Animelib.Core/Users/WatchList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Animelib.Core.Animes;
using RichEntity.Annotations;

namespace Animelib.Core.Users;

public partial class WatchList : IEntity<int>
{
public WatchList(string name, User owner, IReadOnlyList<Anime> animes)
{
Name = name;
Owner = owner;
Animes = animes;
}

public string Name { get; init; }
public User Owner { get; init; }
public IReadOnlyList<Anime> Animes { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Animelib.Core.Animes;
using Animelib.Core.Users;
using Animelib.DataAccess.Abstractions;
using Microsoft.EntityFrameworkCore;

Expand All @@ -13,4 +14,6 @@ public DatabaseContext(DbContextOptions<DatabaseContext> options)
}

public DbSet<Anime> Animes => Set<Anime>();
}
public DbSet<User> Users => Set<User>();
public DbSet<WatchList> WatchLists => Set<WatchList>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static AnimeDto ToDto(this Anime anime)
anime.Subtype
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ public void Dispose()
{
_httpClient.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Animelib.Dto.Identity;
using Animelib.Services.Abstractions;
using Microsoft.AspNetCore.Mvc;

namespace Animelib.Controllers;

[Route("api/auth")]
[ApiController]
public class IdentityController : ControllerBase
{
private readonly IIdentityService _service;

public IdentityController(IIdentityService service)
{
_service = service;
}

[HttpPost("signin")]
public Task<ActionResult> Login([FromBody] LoginUserDto user)
{
return Task.FromResult<ActionResult>(Ok());
}

[HttpPost("signup")]
public async Task<ActionResult> Register([FromBody] CreateUserDto user)
{
await _service.CreateUserAsync(user);
return Ok();
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Animelib.Controllers</RootNamespace>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<UserSecretsId>3c297dff-8f10-408c-9b93-3833133940db</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
<PackageReference Include="ronimizy.FluentSerialization.Extensions.NewtonsoftJson" Version="1.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Application\Animelib.Dto\Animelib.Dto.csproj" />
<ProjectReference Include="..\..\Domain\Animelib.Common\Animelib.Common.csproj" />
<ProjectReference Include="..\..\Infrastructure\Animelib.DataAccess\Animelib.DataAccess.csproj" />
<ProjectReference Include="..\..\Infrastructure\Animelib.Seeding\Animelib.Seeding.csproj" />
<ProjectReference Include="..\Animelib.Controllers\Animelib.Controllers.csproj" />
<ProjectReference Include="..\..\Application\Animelib.Dto\Animelib.Dto.csproj" />
<ProjectReference Include="..\..\Domain\Animelib.Common\Animelib.Common.csproj" />
<ProjectReference Include="..\..\Infrastructure\Animelib.DataAccess\Animelib.DataAccess.csproj" />
<ProjectReference Include="..\..\Infrastructure\Animelib.Seeding\Animelib.Seeding.csproj" />
<ProjectReference Include="..\Animelib.Controllers\Animelib.Controllers.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 75ffe04

Please sign in to comment.