Skip to content

Commit

Permalink
Merge pull request #116 from fluentcms/112-refactor-based-on-sunday-m…
Browse files Browse the repository at this point in the history
…eeting

112 refactor based on sunday meeting
  • Loading branch information
pournasserian authored Nov 5, 2023
2 parents 3d3850d + 9dd9f18 commit 1fc0bcf
Show file tree
Hide file tree
Showing 126 changed files with 1,599 additions and 1,219 deletions.
10 changes: 10 additions & 0 deletions src/FluentCMS.Api/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc;

namespace FluentCMS.Api.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
[Produces("application/json")]
public abstract class BaseController
{
}
52 changes: 52 additions & 0 deletions src/FluentCMS.Api/Controllers/RolesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using AutoMapper;
using FluentCMS.Api.Models;
using FluentCMS.Api.Models.Users;
using FluentCMS.Entities.Users;
using FluentCMS.Services;
using Microsoft.AspNetCore.Mvc;

namespace FluentCMS.Api.Controllers;
public class RolesController(IMapper mapper, IRoleService roleService) : BaseController
{
[HttpGet]
public async Task<IApiPagingResult<RoleResponse>> GetAll([FromQuery] SearchRoleRequest request)
{
var roles = await roleService.GetAll();
var result = mapper.Map<IEnumerable<RoleResponse>>(roles);
return new ApiPagingResult<RoleResponse>(result);
}

[HttpGet("{id}")]
public async Task<IApiResult<RoleResponse>> GetById([FromRoute] Guid id)
{
var role = await roleService.GetById(id);
var result = mapper.Map<RoleResponse>(role);
return new ApiResult<RoleResponse>(result);
}

[HttpPost]
public async Task<IApiResult<RoleResponse>> Create(CreateRoleRequest request)
{
var role = mapper.Map<Role>(request);
var newRole = await roleService.Create(role);
var result = mapper.Map<RoleResponse>(newRole);
return new ApiResult<RoleResponse>(result);
}

[HttpPut]
public async Task<IApiResult<RoleResponse>> Edit(EditRoleRequest request)
{
var role = mapper.Map<Role>(request);
var editedRole = await roleService.Edit(role);
var result = mapper.Map<RoleResponse>(editedRole);
return new ApiResult<RoleResponse>(result);
}

[HttpDelete("{id}")]
public async Task<IApiResult<bool>> Delete([FromRoute] Guid id)
{
var role = await roleService.GetById(id);
await roleService.Delete(role);
return new ApiResult<bool>(true);
}
}
81 changes: 81 additions & 0 deletions src/FluentCMS.Api/Controllers/SiteController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using AutoMapper;
using FluentCMS.Entities.Sites;
using FluentCMS.Api.Models;
using FluentCMS.Api.Models.Sites;
using FluentCMS.Services;
using Microsoft.AspNetCore.Mvc;

namespace FluentCMS.Api.Controllers;

public class SiteController : BaseController
{
private readonly ISiteService _siteService;
private readonly IPageService _pageService;
private readonly IMapper _mapper;

public SiteController(ISiteService siteService, IPageService pageService, IMapper mapper)
{
_siteService = siteService;
_pageService = pageService;
_mapper = mapper;
}

[HttpGet]
public async Task<IApiPagingResult<SiteResponse>> GetAll([FromQuery] SearchSiteRequest request, CancellationToken cancellationToken = default)
{
var sites = await _siteService.GetAll(cancellationToken);
var siteResponses = _mapper.Map<List<SiteResponse>>(sites);
return new ApiPagingResult<SiteResponse>(siteResponses);
}

[HttpGet("{id}")]
public async Task<IApiResult<SiteResponse>> GetById(Guid id, CancellationToken cancellationToken = default)
{
var site = await _siteService.GetById(id, cancellationToken);
var pages = await _pageService.GetBySiteId(site.Id, cancellationToken);

var siteResponse = _mapper.Map<SiteResponse>(site);
siteResponse.Pages = _mapper.Map<List<PageResponse>>(pages);

return new ApiResult<SiteResponse>(siteResponse);
}

[HttpGet]
public async Task<IApiResult<SiteResponse>> GetByUrl([FromQuery] string url, CancellationToken cancellationToken = default)
{
// TODO: should we change Url to domain?
var site = await _siteService.GetByUrl(url, cancellationToken);
var pages = await _pageService.GetBySiteId(site.Id, cancellationToken);

var siteResponse = _mapper.Map<SiteResponse>(site);
siteResponse.Pages = _mapper.Map<List<PageResponse>>(pages);

return new ApiResult<SiteResponse>(siteResponse);
}

[HttpPost]
public async Task<IApiResult<SiteResponse>> Create(CreateSiteRequest request, CancellationToken cancellationToken = default)
{
var site = _mapper.Map<Site>(request);
var newSite = await _siteService.Create(site, cancellationToken);
var siteResponse = _mapper.Map<SiteResponse>(newSite);
return new ApiResult<SiteResponse>(siteResponse);
}

[HttpPatch]
public async Task<IApiResult<SiteResponse>> Update(EditSiteRequest request, CancellationToken cancellationToken = default)
{
var site = _mapper.Map<Site>(request);
var updateSite = await _siteService.Edit(site, cancellationToken);
var siteResponse = _mapper.Map<SiteResponse>(updateSite);
return new ApiResult<SiteResponse>(siteResponse);
}

[HttpDelete("{id}")]
public async Task<IApiResult> Delete([FromRoute] Guid id, CancellationToken cancellationToken = default)
{
var site = await _siteService.GetById(id);
await _siteService.Delete(site, cancellationToken);
return new ApiResult();
}
}
52 changes: 52 additions & 0 deletions src/FluentCMS.Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using AutoMapper;
using FluentCMS.Api.Models;
using FluentCMS.Api.Models.Users;
using FluentCMS.Entities.Users;
using FluentCMS.Services;
using Microsoft.AspNetCore.Mvc;

namespace FluentCMS.Api.Controllers;
public class UsersController(IMapper mapper, IUserService userService) : BaseController
{
[HttpGet]
public async Task<IApiPagingResult<UserResponse>> GetAll([FromQuery] SearchUserRequest request)
{
var users = await userService.GetAll();
var result = mapper.Map<IEnumerable<UserResponse>>(users);
return new ApiPagingResult<UserResponse>(result);
}

[HttpGet("{id}")]
public async Task<IApiResult<UserResponse>> GetById([FromRoute] Guid id)
{
var user = await userService.GetById(id);
var result = mapper.Map<UserResponse>(user);
return new ApiResult<UserResponse>(result);
}

[HttpPost]
public async Task<IApiResult<UserResponse>> Create(CreateUserRequest request)
{
var user = mapper.Map<User>(request);
var newUser = await userService.Create(user);
var result = mapper.Map<UserResponse>(newUser);
return new ApiResult<UserResponse>(result);
}

[HttpPut]
public async Task<IApiResult<UserResponse>> Edit(EditUserRequest request)
{
var user = mapper.Map<User>(request);
var editedUser = await userService.Edit(user);
var result = mapper.Map<UserResponse>(editedUser);
return new ApiResult<UserResponse>(result);
}

[HttpDelete("{id}")]
public async Task<IApiResult<bool>> Delete([FromRoute] Guid id)
{
var user = await userService.GetById(id);
await userService.Delete(user);
return new ApiResult<bool>(true);
}
}
42 changes: 42 additions & 0 deletions src/FluentCMS.Api/FluentCMS.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="SeedData\pages.json" />
<None Remove="SeedData\site.json" />
</ItemGroup>

<ItemGroup>
<Content Include="SeedData\pages.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="SeedData\site.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="FluentValidation" Version="11.8.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.8.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-rc.2.23480.2" />
<PackageReference Include="SharpGrip.FluentValidation.AutoValidation.Mvc" Version="1.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentCMS.Services\FluentCMS.Services.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/FluentCMS.Api/MappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Extensions.DependencyInjection;

namespace FluentCMS.Api;

public static class MappingExtensions
{
public static IServiceCollection AddMappingProfiles(this IServiceCollection services)
{
services.AddAutoMapper(typeof(MappingExtensions).Assembly);

return services;
}
}
30 changes: 30 additions & 0 deletions src/FluentCMS.Api/MappingProfiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using AutoMapper;
using FluentCMS.Api.Models;
using FluentCMS.Api.Models.Sites;
using FluentCMS.Api.Models.Users;
using FluentCMS.Entities.Pages;
using FluentCMS.Entities.Sites;
using FluentCMS.Entities.Users;

namespace FluentCMS.Api;

public class MappingProfiles : Profile
{
public MappingProfiles()
{
// Site
CreateMap<Site, SiteResponse>().ReverseMap();
CreateMap<CreateSiteRequest, Site>();
CreateMap<EditSiteRequest, Site>();

// Page
CreateMap<Page, PageResponse>().ReverseMap();

// User
CreateMap<User, UserResponse>()
.ForMember(x => x.UserRoles, cfg => cfg.MapFrom(y => y.UserRoles.Select(z => z.RoleId.ToString())));

// Role
CreateMap<Role, RoleResponse>();
}
}
54 changes: 54 additions & 0 deletions src/FluentCMS.Api/Models/ApiPagingResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace FluentCMS.Api.Models;

public interface IApiPagingResult<TData> : IApiResult<IEnumerable<TData>>
{
public int PageSize { get; }
public int TotalPages { get; }
public int PageNumber { get; }
public long TotalCount { get; }
public bool HasPrevious { get; }
public bool HasNext { get; }
}

public class ApiPagingResult<TData> : ApiResult<IEnumerable<TData>>, IApiPagingResult<TData>
{
public int PageSize { get; }
public int TotalPages { get; }
public int PageNumber { get; }
public long TotalCount { get; }
public bool HasPrevious { get; }
public bool HasNext { get; }

//public ListResult(IEnumerable<TData> data, int pageNumber, int pageSize, long totalCount)
//{
// Data = data;
// PageNumber = pageNumber;
// PageSize = pageSize;
// TotalCount = totalCount;
// TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
// HasPrevious = PageNumber > 1;
// HasNext = PageNumber < TotalPages;
//}

//public ListResult(IEnumerable<TData> data, int pageNumber, int pageSize, long totalCount, int totalPages, bool hasPrevious, bool hasNext)
//{
// Data = data;
// PageNumber = pageNumber;
// PageSize = pageSize;
// TotalCount = totalCount;
// TotalPages = totalPages;
// HasPrevious = hasPrevious;
// HasNext = hasNext;
//}

public ApiPagingResult(IEnumerable<TData> data)
{
Data = data;
PageNumber = 1;
PageSize = 1;
TotalCount = data.Count();
TotalPages = 1;
HasPrevious = false;
HasNext = false;
}
}
40 changes: 40 additions & 0 deletions src/FluentCMS.Api/Models/ApiResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace FluentCMS.Api.Models;

public interface IApiResult<TData>
{
List<Error> Errors { get; }
List<object> Debug { get; }
string TraceId { get; set; }
string SessionId { get; set; }
double Duration { get; set; }
TData? Data { get; set; }
}

public interface IApiResult : IApiResult<object>
{
}

public class ApiResult<TData> : IApiResult<TData>
{
public List<Error> Errors { get; internal set; } = [];
public List<object> Debug { get; internal set; } = [];
public string TraceId { get; set; } = string.Empty;
public string SessionId { get; set; } = string.Empty;
public double Duration { get; set; } = 0;
public TData? Data { get; set; }

public ApiResult() { }
public ApiResult(TData data)
{
Data = data;
}
}

public class ApiResult : ApiResult<object>, IApiResult
{
public ApiResult() { }
public ApiResult(object data)
{
Data = data;
}
}
7 changes: 7 additions & 0 deletions src/FluentCMS.Api/Models/Error.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FluentCMS.Api.Models;

public class Error
{
public string Code { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
Loading

0 comments on commit 1fc0bcf

Please sign in to comment.