Skip to content

Commit

Permalink
chors: apply services clean
Browse files Browse the repository at this point in the history
  • Loading branch information
mtai0524 committed Nov 18, 2024
1 parent 4d066de commit f795e81
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Notaion.Application.DTOs
namespace Notaion.Application.DTOs.Chats
{
public class GetChatRequest
public class ChatResponseDto
{
public string? Content { get; set; }
public DateTime? SentDate { get; set; }
public string? UserName { get; set; }
public bool IsHiden { get; set; }

}
}
15 changes: 15 additions & 0 deletions NotaionWebApp/Notaion.Application/DTOs/Users/UserResponseDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Application.DTOs.Users
{
public class UserResponseDto
{
public string? UserName { get; set; }
public string? Email { get; set; }
public string? Avatar { get; set; }
}
}
5 changes: 5 additions & 0 deletions NotaionWebApp/Notaion.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Notaion.Application.Mappings;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -13,6 +14,10 @@ public static class DependencyInjection
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());

services.AddAutoMapper(typeof(ChatMappingProfile));
services.AddAutoMapper(typeof(UserMappingProfile));

return services;
}
}
Expand Down
15 changes: 15 additions & 0 deletions NotaionWebApp/Notaion.Application/Interfaces/IChatService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Notaion.Application.DTOs.Chats;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Application.Interfaces
{
public interface IChatService
{
Task<List<ChatResponseDto>> GetChatsAsync();
Task<List<ChatResponseDto>> GetChatsHiddenAsync();
}
}
20 changes: 0 additions & 20 deletions NotaionWebApp/Notaion.Application/Mapper/ChatProfile.cs

This file was deleted.

15 changes: 15 additions & 0 deletions NotaionWebApp/Notaion.Application/Mappings/ChatMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AutoMapper;
using Notaion.Application.DTOs.Chats;
using Notaion.Domain.Entities;

namespace Notaion.Application.Mappings
{
public class ChatMappingProfile : Profile
{
public ChatMappingProfile()
{
CreateMap<Chat, ChatResponseDto>()
.ForMember(dest => dest.IsHiden, opt => opt.MapFrom(src => src.Hide)); // config auto mapper - prefix
}
}
}
14 changes: 14 additions & 0 deletions NotaionWebApp/Notaion.Application/Mappings/UserMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AutoMapper;
using Notaion.Application.DTOs.Users;
using Notaion.Domain.Entities;

namespace Notaion.Application.Mappings
{
public class UserMappingProfile : Profile
{
public UserMappingProfile()
{
CreateMap<User, UserResponseDto>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand Down
34 changes: 34 additions & 0 deletions NotaionWebApp/Notaion.Application/Services/ChatService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using AutoMapper;
using Notaion.Application.DTOs.Chats;
using Notaion.Application.Interfaces;
using Notaion.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Application.Services
{
public class ChatService : IChatService
{
private readonly IChatRepository _chatRepository;
private readonly IMapper _mapper;
public ChatService(IChatRepository chatRepository, IMapper mapper)
{
_chatRepository = chatRepository;
_mapper = mapper;
}
public async Task<List<ChatResponseDto>> GetChatsAsync()
{
var chats = await _chatRepository.GetChatsAsync();
return _mapper.Map<List<ChatResponseDto>>(chats);
}

public async Task<List<ChatResponseDto>> GetChatsHiddenAsync()
{
var chats = await _chatRepository.GetChatsHiddenAsync();
return _mapper.Map<List<ChatResponseDto>>(chats);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Application.Repositories
namespace Notaion.Domain.Interfaces
{
public interface IAccountRepository
{
public Task<IdentityResult> SignUpAsync(SignUpModel model);
public Task<string> SignInAsync(SignInModel model);
Task<IEnumerable<object>> GetAllUsersAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Notaion.Application.DTOs;
using Notaion.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Application.Repositories
namespace Notaion.Domain.Interfaces
{
public interface IChatRepository
{
List<GetChatRequest> GetChats();
List<GetChatRequest> GetChatsHide();
Task<List<Chat>> GetChatsAsync();
Task<List<Chat>> GetChatsHiddenAsync();
}
}
11 changes: 9 additions & 2 deletions NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Notaion.Application.Common.Interfaces;
using Notaion.Application.Repositories;
using Notaion.Application.Interfaces;
using Notaion.Application.Services;
using Notaion.Domain.Interfaces;
using Notaion.Infrastructure.Context;
using Notaion.Infrastructure.Persistence;
using Notaion.Infrastructure.Services;
Expand All @@ -26,10 +28,15 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}, ServiceLifetime.Singleton, ServiceLifetime.Transient);

// chat
services.AddScoped<IChatRepository, ChatRepository>();
services.AddScoped<ICloudinaryService, CloudinaryService>();
services.AddScoped<IChatService, ChatService>();

// account user
services.AddScoped<IAccountRepository, AccountRepository>();

services.AddScoped<ICloudinaryService, CloudinaryService>();

return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using Microsoft.AspNetCore.Identity;
using AutoMapper;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Notaion.Application.Repositories;
using Notaion.Application.DTOs.Chats;
using Notaion.Application.DTOs.Users;
using Notaion.Domain.Entities;
using Notaion.Domain.Interfaces;
using Notaion.Domain.Models;
using Notaion.Infrastructure.Context;
using Notaion.Models;
using System;
using System.Collections.Generic;
Expand All @@ -20,12 +25,22 @@ public class AccountRepository : IAccountRepository
private readonly UserManager<User> userManager;
private readonly SignInManager<User> signInManager;
private readonly IConfiguration configuration;
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;

public AccountRepository(UserManager<User> userManager, SignInManager<User> signInManager, IConfiguration configuration)
public AccountRepository(UserManager<User> userManager, SignInManager<User> signInManager, IConfiguration configuration, ApplicationDbContext context, IMapper mapper)
{
this.userManager = userManager;
this.signInManager = signInManager;
this.configuration = configuration;
_context = context;
_mapper = mapper;
}

public async Task<IEnumerable<object>> GetAllUsersAsync()
{
return await _context.User
.Select(p => this._mapper.Map<UserResponseDto>(p)).ToListAsync();
}

public async Task<string> SignInAsync(SignInModel model) // đăng nhập
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using AutoMapper;
using Notaion.Application.DTOs;
using Notaion.Application.Repositories;
using Microsoft.EntityFrameworkCore;
using Notaion.Application.DTOs.Chats;
using Notaion.Domain.Entities;
using Notaion.Domain.Interfaces;
using Notaion.Infrastructure.Context;
using System;
using System.Collections.Generic;
Expand All @@ -20,22 +21,22 @@ public ChatRepository(ApplicationDbContext context, IMapper mapper)
_context = context;
_mapper = mapper;
}
public List<GetChatRequest> GetChats()

public async Task<List<Chat>> GetChatsHiddenAsync()
{
return this._context.Chat
.OrderBy(c => c.SentDate)
.Where(x => x.Hide == false)
.ToList()
.Select(p => this._mapper.Map<GetChatRequest>(p)).ToList();
return _mapper.Map<List<Chat>>(await _context.Chat
.Where(c => c.Hide == true)
.OrderBy(c => c.SentDate)
.ToListAsync());
}

public List<GetChatRequest> GetChatsHide()
async Task<List<Chat>> IChatRepository.GetChatsAsync()
{
return this._context.Chat
return _mapper.Map<List<Chat>>(await _context.Chat
.Where(c => c.Hide == false)
.OrderBy(c => c.SentDate)
.Where(x => x.Hide == true)
.ToList()
.Select(p => this._mapper.Map<GetChatRequest>(p)).ToList();
.ToListAsync());
}

}
}
2 changes: 1 addition & 1 deletion NotaionWebApp/Notaion/Controllers/ApiAccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Notaion.Hubs;
using Notaion.Domain.Entities;
using Notaion.Domain.Models;
using Notaion.Application.Repositories;
using Notaion.Domain.Interfaces;

namespace WebAPI.Controllers
{
Expand Down
16 changes: 8 additions & 8 deletions NotaionWebApp/Notaion/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Notaion.Domain.Models;
using AutoMapper;
using Notaion.Application.DTOs;
using Notaion.Application.Repositories;
using Notaion.Application.Interfaces;

namespace Notaion.Controllers
{
Expand All @@ -22,25 +22,25 @@ public class ChatController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly IHubContext<ChatHub> _hubContext;
private readonly IChatRepository chatRepository;
private readonly IChatService chatService;

public ChatController(ApplicationDbContext context, IHubContext<ChatHub> hubContext, IChatRepository chatRepository)
public ChatController(ApplicationDbContext context, IHubContext<ChatHub> hubContext, IChatService chatService)
{
_context = context;
_hubContext = hubContext;
this.chatRepository = chatRepository;
this.chatService = chatService;
}
//[Authorize]
[HttpGet("get-chats")]
public IActionResult GetChats()
public async Task<IActionResult> GetChats()
{
return Ok(this.chatRepository.GetChats());
return Ok(await this.chatService.GetChatsAsync());
}

[HttpGet("get-chats-hidden")]
public IActionResult GetChatsHidden()
public async Task<IActionResult> GetChatsHidden()
{
return Ok(this.chatRepository.GetChatsHide());
return Ok(await this.chatService.GetChatsHiddenAsync());
}

[HttpPost("add-chat")]
Expand Down
10 changes: 2 additions & 8 deletions NotaionWebApp/Notaion/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Text;
using Notaion.Application;
using Notaion.Infrastructure;
using Notaion.Application.Mapper;
using Notaion.Infrastructure.Options;
using Notaion.Domain.Entities;

Expand All @@ -25,9 +24,6 @@
builder.Services.AddApplication();
builder.Services.AddInfrastructure(builder.Configuration);

// mapper
builder.Services.AddAutoMapper(typeof(ChatProfile));

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Expand All @@ -54,8 +50,6 @@
builder.Services.Configure<CloudinaryOptions>(builder.Configuration.GetSection("Cloudinary"));

// Services inject
builder.Services.AddTransient<ApplicationDbContext>();

builder.Services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
Expand Down Expand Up @@ -137,8 +131,8 @@

//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwagger();
app.UseSwaggerUI();
//}
app.MapHub<ChatHub>("/chatHub");

Expand Down
Loading

0 comments on commit f795e81

Please sign in to comment.