From 5655bba07a8c9d0415c74d75608033177068fae8 Mon Sep 17 00:00:00 2001 From: mtai0524 Date: Sun, 17 Nov 2024 22:54:40 +0700 Subject: [PATCH] chors: use interface repo, config cloud clean architecture --- .../Infrastructure/Infrastructure.csproj | 19 --------- .../Common/Interfaces/ICloudinaryService.cs | 14 +++++++ .../DTOs/GetChatRequest.cs | 18 ++++++++ .../DependencyInjection.cs | 19 +++++++++ .../Notaion.Application/Mapper/ChatProfile.cs | 20 +++++++++ .../Notaion.Application.csproj | 6 +++ .../Repositories/IChatRepository.cs | 15 +++++++ NotaionWebApp/Notaion.Domain/Entities/User.cs | 14 +++++++ NotaionWebApp/Notaion.Domain/Models/User.cs | 9 ---- .../DependencyInjection.cs | 34 +++++++++++++++ .../Notaion.Infrastructure.csproj | 3 ++ .../Options/CloudinaryOptions.cs | 15 +++++++ .../Persistence/ChatRepository.cs | 41 +++++++++++++++++++ .../Services/CloudinaryService.cs | 28 +++++++------ .../Configurations/CloudinarySetting.cs | 9 ---- .../Notaion/Controllers/ChatController.cs | 38 ++++------------- .../Notaion/Controllers/ItemsController.cs | 3 +- NotaionWebApp/Notaion/Helpers/AuthService.cs | 22 ---------- .../Notaion/Helpers/CustomSignInManager.cs | 6 --- NotaionWebApp/Notaion/Helpers/LoginHandler.cs | 20 --------- NotaionWebApp/Notaion/Hubs/ChatHub.cs | 7 ---- .../{Notaion.API.csproj => Notaion.csproj} | 6 +-- ...on.API.csproj.user => Notaion.csproj.user} | 0 NotaionWebApp/Notaion/Program.cs | 34 ++++++--------- .../Notaion/Repositories/AccountRepository.cs | 3 +- NotaionWebApp/Notaion/Services/UserService.cs | 38 ----------------- NotaionWebApp/Notaion/appsettings.json | 7 ---- NotaionWebApp/NotionWebApp.sln | 6 +-- 28 files changed, 242 insertions(+), 212 deletions(-) delete mode 100644 NotaionWebApp/Infrastructure/Infrastructure.csproj create mode 100644 NotaionWebApp/Notaion.Application/Common/Interfaces/ICloudinaryService.cs create mode 100644 NotaionWebApp/Notaion.Application/DTOs/GetChatRequest.cs create mode 100644 NotaionWebApp/Notaion.Application/DependencyInjection.cs create mode 100644 NotaionWebApp/Notaion.Application/Mapper/ChatProfile.cs create mode 100644 NotaionWebApp/Notaion.Application/Repositories/IChatRepository.cs create mode 100644 NotaionWebApp/Notaion.Domain/Entities/User.cs delete mode 100644 NotaionWebApp/Notaion.Domain/Models/User.cs create mode 100644 NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs create mode 100644 NotaionWebApp/Notaion.Infrastructure/Options/CloudinaryOptions.cs create mode 100644 NotaionWebApp/Notaion.Infrastructure/Persistence/ChatRepository.cs rename NotaionWebApp/{Notaion => Notaion.Infrastructure}/Services/CloudinaryService.cs (71%) delete mode 100644 NotaionWebApp/Notaion/Configurations/CloudinarySetting.cs delete mode 100644 NotaionWebApp/Notaion/Helpers/AuthService.cs delete mode 100644 NotaionWebApp/Notaion/Helpers/CustomSignInManager.cs delete mode 100644 NotaionWebApp/Notaion/Helpers/LoginHandler.cs rename NotaionWebApp/Notaion/{Notaion.API.csproj => Notaion.csproj} (95%) rename NotaionWebApp/Notaion/{Notaion.API.csproj.user => Notaion.csproj.user} (100%) delete mode 100644 NotaionWebApp/Notaion/Services/UserService.cs diff --git a/NotaionWebApp/Infrastructure/Infrastructure.csproj b/NotaionWebApp/Infrastructure/Infrastructure.csproj deleted file mode 100644 index 33be330..0000000 --- a/NotaionWebApp/Infrastructure/Infrastructure.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - net7.0 - enable - enable - - - - - - - - - - - - - diff --git a/NotaionWebApp/Notaion.Application/Common/Interfaces/ICloudinaryService.cs b/NotaionWebApp/Notaion.Application/Common/Interfaces/ICloudinaryService.cs new file mode 100644 index 0000000..f4fc3cb --- /dev/null +++ b/NotaionWebApp/Notaion.Application/Common/Interfaces/ICloudinaryService.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Application.Common.Interfaces +{ + public interface ICloudinaryService + { + Task UploadImageAsync(IFormFile imageFile); + } +} diff --git a/NotaionWebApp/Notaion.Application/DTOs/GetChatRequest.cs b/NotaionWebApp/Notaion.Application/DTOs/GetChatRequest.cs new file mode 100644 index 0000000..f4a9799 --- /dev/null +++ b/NotaionWebApp/Notaion.Application/DTOs/GetChatRequest.cs @@ -0,0 +1,18 @@ +using Notaion.Domain.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Application.DTOs +{ + public class GetChatRequest + { + public string? Content { get; set; } + public DateTime? SentDate { get; set; } + public string? UserName { get; set; } + public bool IsHiden { get; set; } + } +} diff --git a/NotaionWebApp/Notaion.Application/DependencyInjection.cs b/NotaionWebApp/Notaion.Application/DependencyInjection.cs new file mode 100644 index 0000000..334d2cd --- /dev/null +++ b/NotaionWebApp/Notaion.Application/DependencyInjection.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Application +{ + public static class DependencyInjection + { + public static IServiceCollection AddApplication(this IServiceCollection services) + { + services.AddAutoMapper(Assembly.GetExecutingAssembly()); + return services; + } + } +} diff --git a/NotaionWebApp/Notaion.Application/Mapper/ChatProfile.cs b/NotaionWebApp/Notaion.Application/Mapper/ChatProfile.cs new file mode 100644 index 0000000..ae86a25 --- /dev/null +++ b/NotaionWebApp/Notaion.Application/Mapper/ChatProfile.cs @@ -0,0 +1,20 @@ +using AutoMapper; +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.Mapper +{ + public class ChatProfile : Profile + { + public ChatProfile() + { + CreateMap() + .ForMember(dest => dest.IsHiden, opt => opt.MapFrom(src => src.Hide)); // config auto mapper - prefix + } + } +} diff --git a/NotaionWebApp/Notaion.Application/Notaion.Application.csproj b/NotaionWebApp/Notaion.Application/Notaion.Application.csproj index 775c1b6..243d180 100644 --- a/NotaionWebApp/Notaion.Application/Notaion.Application.csproj +++ b/NotaionWebApp/Notaion.Application/Notaion.Application.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/NotaionWebApp/Notaion.Application/Repositories/IChatRepository.cs b/NotaionWebApp/Notaion.Application/Repositories/IChatRepository.cs new file mode 100644 index 0000000..8afe9ab --- /dev/null +++ b/NotaionWebApp/Notaion.Application/Repositories/IChatRepository.cs @@ -0,0 +1,15 @@ +using Notaion.Application.DTOs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Application.Repositories +{ + public interface IChatRepository + { + List GetChats(); + List GetChatsHide(); + } +} diff --git a/NotaionWebApp/Notaion.Domain/Entities/User.cs b/NotaionWebApp/Notaion.Domain/Entities/User.cs new file mode 100644 index 0000000..e63c11b --- /dev/null +++ b/NotaionWebApp/Notaion.Domain/Entities/User.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Identity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Domain.Entities +{ + public class User : IdentityUser + { + public string? Avatar { get; set; } + } +} diff --git a/NotaionWebApp/Notaion.Domain/Models/User.cs b/NotaionWebApp/Notaion.Domain/Models/User.cs deleted file mode 100644 index 625849e..0000000 --- a/NotaionWebApp/Notaion.Domain/Models/User.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Microsoft.AspNetCore.Identity; - -namespace Notaion.Domain.Models -{ - public class User : IdentityUser - { - public string? Avatar { get; set; } - } -} diff --git a/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs b/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs new file mode 100644 index 0000000..d5f7fe4 --- /dev/null +++ b/NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Notaion.Application.Common.Interfaces; +using Notaion.Application.Repositories; +using Notaion.Infrastructure.Context; +using Notaion.Infrastructure.Persistence; +using Notaion.Infrastructure.Services; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Infrastructure +{ + public static class DependencyInjection + { + public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) + { + var connectionString = configuration.GetConnectionString("DefaultConnection"); + + services.AddDbContext(options => + { + options.UseSqlServer(connectionString); + options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); + }, ServiceLifetime.Singleton, ServiceLifetime.Transient); + + services.AddScoped(); + services.AddScoped(); + return services; + } + } +} diff --git a/NotaionWebApp/Notaion.Infrastructure/Notaion.Infrastructure.csproj b/NotaionWebApp/Notaion.Infrastructure/Notaion.Infrastructure.csproj index 504f285..7d6c376 100644 --- a/NotaionWebApp/Notaion.Infrastructure/Notaion.Infrastructure.csproj +++ b/NotaionWebApp/Notaion.Infrastructure/Notaion.Infrastructure.csproj @@ -7,6 +7,9 @@ + + + diff --git a/NotaionWebApp/Notaion.Infrastructure/Options/CloudinaryOptions.cs b/NotaionWebApp/Notaion.Infrastructure/Options/CloudinaryOptions.cs new file mode 100644 index 0000000..4eb248e --- /dev/null +++ b/NotaionWebApp/Notaion.Infrastructure/Options/CloudinaryOptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Infrastructure.Options +{ + public class CloudinaryOptions + { + public string CloudName { get; set; } + public string ApiKey { get; set; } + public string ApiSecret { get; set; } + } +} diff --git a/NotaionWebApp/Notaion.Infrastructure/Persistence/ChatRepository.cs b/NotaionWebApp/Notaion.Infrastructure/Persistence/ChatRepository.cs new file mode 100644 index 0000000..eaf41df --- /dev/null +++ b/NotaionWebApp/Notaion.Infrastructure/Persistence/ChatRepository.cs @@ -0,0 +1,41 @@ +using AutoMapper; +using Notaion.Application.DTOs; +using Notaion.Application.Repositories; +using Notaion.Domain.Entities; +using Notaion.Infrastructure.Context; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Notaion.Infrastructure.Persistence +{ + public class ChatRepository : IChatRepository + { + private readonly ApplicationDbContext _context; + private readonly IMapper _mapper; + public ChatRepository(ApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + public List GetChats() + { + return this._context.Chat + .OrderBy(c => c.SentDate) + .Where(x => x.Hide == false) + .ToList() + .Select(p => this._mapper.Map(p)).ToList(); + } + + public List GetChatsHide() + { + return this._context.Chat + .OrderBy(c => c.SentDate) + .Where(x => x.Hide == true) + .ToList() + .Select(p => this._mapper.Map(p)).ToList(); + } + } +} diff --git a/NotaionWebApp/Notaion/Services/CloudinaryService.cs b/NotaionWebApp/Notaion.Infrastructure/Services/CloudinaryService.cs similarity index 71% rename from NotaionWebApp/Notaion/Services/CloudinaryService.cs rename to NotaionWebApp/Notaion.Infrastructure/Services/CloudinaryService.cs index d24a8c7..cc62836 100644 --- a/NotaionWebApp/Notaion/Services/CloudinaryService.cs +++ b/NotaionWebApp/Notaion.Infrastructure/Services/CloudinaryService.cs @@ -1,26 +1,30 @@ using CloudinaryDotNet; using CloudinaryDotNet.Actions; -using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; -using Notaion.Configurations; -using System.IO; +using Notaion.Application.Common.Interfaces; +using Notaion.Infrastructure.Options; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; using System.Threading.Tasks; -namespace Notaion.Services +namespace Notaion.Infrastructure.Services { - public interface ICloudinaryService - { - Task UploadImageAsync(IFormFile imageFile); - } - public class CloudinaryService : ICloudinaryService { private readonly Cloudinary _cloudinary; - public CloudinaryService(Cloudinary cloudinary) + public CloudinaryService(IOptions options) { - _cloudinary = cloudinary; + var account = new Account( + options.Value.CloudName, + options.Value.ApiKey, + options.Value.ApiSecret + ); + _cloudinary = new Cloudinary(account); } [RequestSizeLimit(1024 * 1024 * 100)] @@ -44,7 +48,7 @@ public async Task UploadImageAsync(IFormFile imageFile) if (uploadResult.StatusCode == System.Net.HttpStatusCode.OK) { - return uploadResult.SecureUri.AbsoluteUri; + return uploadResult.SecureUrl.AbsoluteUri; } else { diff --git a/NotaionWebApp/Notaion/Configurations/CloudinarySetting.cs b/NotaionWebApp/Notaion/Configurations/CloudinarySetting.cs deleted file mode 100644 index 11f5000..0000000 --- a/NotaionWebApp/Notaion/Configurations/CloudinarySetting.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Notaion.Configurations -{ - public class CloudinarySetting - { - public string CloudName { get; set; } - public string ApiKey { get; set; } - public string ApiSecret { get; set; } - } -} diff --git a/NotaionWebApp/Notaion/Controllers/ChatController.cs b/NotaionWebApp/Notaion/Controllers/ChatController.cs index e7e0b33..b75a66b 100644 --- a/NotaionWebApp/Notaion/Controllers/ChatController.cs +++ b/NotaionWebApp/Notaion/Controllers/ChatController.cs @@ -4,13 +4,15 @@ using Notaion.Infrastructure.Context; using Notaion.Domain.Entities; using Notaion.Models; -using Notaion.Services; using Notaion.Hubs; using System.Threading.Tasks; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authorization; using Notaion.Domain.Models; +using AutoMapper; +using Notaion.Application.DTOs; +using Notaion.Application.Repositories; namespace Notaion.Controllers { @@ -20,49 +22,25 @@ public class ChatController : ControllerBase { private readonly ApplicationDbContext _context; private readonly IHubContext _hubContext; + private readonly IChatRepository chatRepository; - public ChatController(ApplicationDbContext context, IHubContext hubContext) + public ChatController(ApplicationDbContext context, IHubContext hubContext, IChatRepository chatRepository) { _context = context; _hubContext = hubContext; + this.chatRepository = chatRepository; } //[Authorize] [HttpGet("get-chats")] public IActionResult GetChats() { - var chats = _context.Chat - .Select(c => new - { - c.Id, - c.Content, - c.SentDate, - UserName = c.User.UserName, - c.Hide - }) - .OrderBy(c => c.SentDate) - .Where(x => x.Hide == false) - .ToList(); - - return Ok(chats); + return Ok(this.chatRepository.GetChats()); } [HttpGet("get-chats-hidden")] public IActionResult GetChatsHidden() { - var chats = _context.Chat - .Select(c => new - { - c.Id, - c.Content, - c.SentDate, - UserName = c.User.UserName, - c.Hide - }) - .OrderBy(c => c.SentDate) - .Where(x => x.Hide == true) - .ToList(); - - return Ok(chats); + return Ok(this.chatRepository.GetChatsHide()); } [HttpPost("add-chat")] diff --git a/NotaionWebApp/Notaion/Controllers/ItemsController.cs b/NotaionWebApp/Notaion/Controllers/ItemsController.cs index 6c0dbc1..fa2d69c 100644 --- a/NotaionWebApp/Notaion/Controllers/ItemsController.cs +++ b/NotaionWebApp/Notaion/Controllers/ItemsController.cs @@ -3,10 +3,9 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; -using Notaion.Configurations; using Notaion.Infrastructure.Context; -using Notaion.Services; using Notaion.Domain.Entities; +using Notaion.Application.Common.Interfaces; namespace Notaion.API.Controllers { diff --git a/NotaionWebApp/Notaion/Helpers/AuthService.cs b/NotaionWebApp/Notaion/Helpers/AuthService.cs deleted file mode 100644 index 839611c..0000000 --- a/NotaionWebApp/Notaion/Helpers/AuthService.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.AspNetCore.SignalR; -using Notaion.Hubs; - -namespace Notaion.Helpers -{ - public class AuthService - { - private readonly IHubContext _hubContext; - - public AuthService(IHubContext hubContext) - { - _hubContext = hubContext; - } - - public async Task OnUserLoggedIn(string userName) - { - // Notify all clients that a user has logged in - await _hubContext.Clients.All.SendAsync("ReceiveNotification", $"{userName} has logged in."); - } - } - -} diff --git a/NotaionWebApp/Notaion/Helpers/CustomSignInManager.cs b/NotaionWebApp/Notaion/Helpers/CustomSignInManager.cs deleted file mode 100644 index a880a48..0000000 --- a/NotaionWebApp/Notaion/Helpers/CustomSignInManager.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Notaion.Helpers -{ - public class CustomSignInManager - { - } -} diff --git a/NotaionWebApp/Notaion/Helpers/LoginHandler.cs b/NotaionWebApp/Notaion/Helpers/LoginHandler.cs deleted file mode 100644 index b776b9c..0000000 --- a/NotaionWebApp/Notaion/Helpers/LoginHandler.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.AspNetCore.SignalR; -using Notaion.Hubs; - -namespace Notaion.Helpers -{ - public class LoginHandler - { - private readonly IHubContext _hubContext; - - public LoginHandler(IHubContext hubContext) - { - _hubContext = hubContext; - } - - public async Task OnUserLoggedIn(string userName) - { - await _hubContext.Clients.All.SendAsync("UserLoggedIn", userName); - } - } -} diff --git a/NotaionWebApp/Notaion/Hubs/ChatHub.cs b/NotaionWebApp/Notaion/Hubs/ChatHub.cs index 187508e..ae049de 100644 --- a/NotaionWebApp/Notaion/Hubs/ChatHub.cs +++ b/NotaionWebApp/Notaion/Hubs/ChatHub.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.CodeAnalysis.Elfie.Serialization; using Notaion.Models; -using Notaion.Services; using System.Collections.Concurrent; using System.Security.Claims; @@ -51,12 +50,6 @@ public async Task CreateGroup(string groupName) await Groups.AddToGroupAsync(Context.ConnectionId, groupName); await Clients.Group(groupName).SendAsync("ReceiveMessage", "System", $"{Context.ConnectionId} has created and joined the group {groupName}."); } - private readonly UserService _userService; - - public ChatHub(UserService userService) - { - _userService = userService; - } private static ConcurrentDictionary OnlineUsers = new ConcurrentDictionary(); diff --git a/NotaionWebApp/Notaion/Notaion.API.csproj b/NotaionWebApp/Notaion/Notaion.csproj similarity index 95% rename from NotaionWebApp/Notaion/Notaion.API.csproj rename to NotaionWebApp/Notaion/Notaion.csproj index a556f24..8bf7d31 100644 --- a/NotaionWebApp/Notaion/Notaion.API.csproj +++ b/NotaionWebApp/Notaion/Notaion.csproj @@ -1,4 +1,4 @@ - + net7.0 @@ -34,8 +34,4 @@ - - - - diff --git a/NotaionWebApp/Notaion/Notaion.API.csproj.user b/NotaionWebApp/Notaion/Notaion.csproj.user similarity index 100% rename from NotaionWebApp/Notaion/Notaion.API.csproj.user rename to NotaionWebApp/Notaion/Notaion.csproj.user diff --git a/NotaionWebApp/Notaion/Program.cs b/NotaionWebApp/Notaion/Program.cs index e7d264f..44ba163 100644 --- a/NotaionWebApp/Notaion/Program.cs +++ b/NotaionWebApp/Notaion/Program.cs @@ -8,25 +8,26 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Identity.Web; using Microsoft.IdentityModel.Tokens; -using Notaion.Configurations; using Notaion.Infrastructure.Context; using Notaion.Domain.Models; using Notaion.Hubs; using Notaion.Models; using Notaion.Repositories; -using Notaion.Services; using System.Text; +using Notaion.Application; +using Notaion.Infrastructure; +using Notaion.Application.Mapper; +using Notaion.Infrastructure.Options; +using Notaion.Domain.Entities; var builder = WebApplication.CreateBuilder(args); -var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); - -builder.Services.AddDbContext(options => -{ - options.UseSqlServer(connectionString); - options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); // fix lỗi lúc không chọn ảnh, chỉ update thong tin khác -}, ServiceLifetime.Singleton, ServiceLifetime.Transient); +// clean architecture +builder.Services.AddApplication(); +builder.Services.AddInfrastructure(builder.Configuration); +// mapper +builder.Services.AddAutoMapper(typeof(ChatProfile)); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); @@ -43,20 +44,10 @@ .AllowCredentials()); }); -//// Cấu hình tài khoản Cloudinary -var configuration = builder.Configuration; - -var cloudName = configuration["Cloudinary:CloudName"]; -var apiKey = configuration["Cloudinary:ApiKey"]; -var apiSecret = configuration["Cloudinary:ApiSecret"]; - -var cloudinaryAccount = new Account(cloudName, apiKey, apiSecret); -var cloudinary = new Cloudinary(cloudinaryAccount); +// cloud +builder.Services.Configure(builder.Configuration.GetSection("Cloudinary")); // Services inject -builder.Services.AddScoped(); -builder.Services.AddSingleton(cloudinary); -builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddTransient(); @@ -65,6 +56,7 @@ .AddDefaultTokenProviders(); builder.Services.AddHttpClient(); builder.Services.AddSingleton(); + // Session builder.Services.AddDistributedMemoryCache(); builder.Services.AddSession(); diff --git a/NotaionWebApp/Notaion/Repositories/AccountRepository.cs b/NotaionWebApp/Notaion/Repositories/AccountRepository.cs index 2e23552..8d22192 100644 --- a/NotaionWebApp/Notaion/Repositories/AccountRepository.cs +++ b/NotaionWebApp/Notaion/Repositories/AccountRepository.cs @@ -6,8 +6,8 @@ using System.Text; using CloudinaryDotNet; using CloudinaryDotNet.Actions; -using Notaion.Services; using Notaion.Domain.Models; +using Notaion.Domain.Entities; namespace Notaion.Repositories { @@ -17,7 +17,6 @@ public class AccountRepository : IAccountRepository private readonly SignInManager signInManager; private readonly IConfiguration configuration; - public AccountRepository(UserManager userManager, SignInManager signInManager, IConfiguration configuration) { this.userManager = userManager; diff --git a/NotaionWebApp/Notaion/Services/UserService.cs b/NotaionWebApp/Notaion/Services/UserService.cs deleted file mode 100644 index ad7a21f..0000000 --- a/NotaionWebApp/Notaion/Services/UserService.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.AspNetCore.Identity; -using Microsoft.EntityFrameworkCore; -using Notaion.Infrastructure.Context; -using Notaion.Domain.Models; -using Notaion.Models; - -namespace Notaion.Services -{ - public class UserService - { - private readonly UserManager _userManager; - private readonly IHttpContextAccessor _httpContextAccessor; - private readonly ApplicationDbContext _context; - public UserService(UserManager userManager, IHttpContextAccessor httpContextAccessor, ApplicationDbContext context) - { - _userManager = userManager; - _httpContextAccessor = httpContextAccessor; - _context = context; - } - - public async Task GetUserNameByIdAsync(string userId) - { - var user = await _context.Users - .Where(x => x.Id == userId) - .Select(x => x.UserName) - .FirstOrDefaultAsync(); - - return user; - } - - - public async Task GetCurrentLoggedInUser() - { - var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); - return user; - } - } -} diff --git a/NotaionWebApp/Notaion/appsettings.json b/NotaionWebApp/Notaion/appsettings.json index 79d9b5a..1868785 100644 --- a/NotaionWebApp/Notaion/appsettings.json +++ b/NotaionWebApp/Notaion/appsettings.json @@ -1,16 +1,9 @@ { "ConnectionStrings": { - //"DefaultConnection": "Server=db8008.databaseasp.net; Database=db8008; User Id=db8008; Password=qZ+67gS%k_2M; Encrypt=False; MultipleActiveResultSets=True;", "DefaultConnection": "workstation id=notiondb.mssql.somee.com;packet size=4096;user id=notaion_SQLLogin_1;pwd=bjzmobsh5c;data source=notiondb.mssql.somee.com;persist security info=False;initial catalog=notiondb;TrustServerCertificate=True" - //"DefaultConnection": "server=.;database=notaion;Integrated Security=true;TrustServerCertificate=True;MultipleActiveResultSets=true;" }, - "Cloudinary": { - "CloudName": "dl3hvap4a", - "ApiKey": "834354428788744", - "ApiSecret": "lv7zI6VPru0YhHwUPQsru318SOE" - }, "Logging": { "LogLevel": { diff --git a/NotaionWebApp/NotionWebApp.sln b/NotaionWebApp/NotionWebApp.sln index 0e11b50..845e59f 100644 --- a/NotaionWebApp/NotionWebApp.sln +++ b/NotaionWebApp/NotionWebApp.sln @@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34511.84 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notaion.API", "Notaion\Notaion.API.csproj", "{EF90AC04-F263-48D1-AC7F-50E4D835E8D6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notaion", "Notaion\Notaion.csproj", "{EF90AC04-F263-48D1-AC7F-50E4D835E8D6}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notaion.Domain", "Notaion.Domain\Notaion.Domain.csproj", "{20E32AC7-43D6-450C-A848-22C0012BD4D9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notaion.Application", "Notaion.Application\Notaion.Application.csproj", "{B4D437BD-AB28-46DE-ADA7-EEBDCD8243E0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notaion.Application", "Notaion.Application\Notaion.Application.csproj", "{B4D437BD-AB28-46DE-ADA7-EEBDCD8243E0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notaion.Infrastructure", "Notaion.Infrastructure\Notaion.Infrastructure.csproj", "{5B06367C-DF30-4552-8F2F-A5099BAEA757}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notaion.Infrastructure", "Notaion.Infrastructure\Notaion.Infrastructure.csproj", "{5B06367C-DF30-4552-8F2F-A5099BAEA757}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution