Skip to content

Commit

Permalink
Merge pull request #2 from mtai0524/dev
Browse files Browse the repository at this point in the history
chors: use interface repo, config cloud clean architecture
  • Loading branch information
mtai0524 authored Nov 17, 2024
2 parents 4bea028 + 5655bba commit fb80c4e
Show file tree
Hide file tree
Showing 28 changed files with 242 additions and 212 deletions.
19 changes: 0 additions & 19 deletions NotaionWebApp/Infrastructure/Infrastructure.csproj

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<string> UploadImageAsync(IFormFile imageFile);
}
}
18 changes: 18 additions & 0 deletions NotaionWebApp/Notaion.Application/DTOs/GetChatRequest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
19 changes: 19 additions & 0 deletions NotaionWebApp/Notaion.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
20 changes: 20 additions & 0 deletions NotaionWebApp/Notaion.Application/Mapper/ChatProfile.cs
Original file line number Diff line number Diff line change
@@ -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<Chat, GetChatRequest>()
.ForMember(dest => dest.IsHiden, opt => opt.MapFrom(src => src.Hide)); // config auto mapper - prefix
}
}
}
6 changes: 6 additions & 0 deletions NotaionWebApp/Notaion.Application/Notaion.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Notaion.Domain\Notaion.Domain.csproj" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions NotaionWebApp/Notaion.Application/Repositories/IChatRepository.cs
Original file line number Diff line number Diff line change
@@ -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<GetChatRequest> GetChats();
List<GetChatRequest> GetChatsHide();
}
}
14 changes: 14 additions & 0 deletions NotaionWebApp/Notaion.Domain/Entities/User.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
9 changes: 0 additions & 9 deletions NotaionWebApp/Notaion.Domain/Models/User.cs

This file was deleted.

34 changes: 34 additions & 0 deletions NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -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<ApplicationDbContext>(options =>
{
options.UseSqlServer(connectionString);
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}, ServiceLifetime.Singleton, ServiceLifetime.Transient);

services.AddScoped<IChatRepository, ChatRepository>();
services.AddScoped<ICloudinaryService, CloudinaryService>();
return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CloudinaryDotNet" Version="1.26.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.20" />
</ItemGroup>

Expand Down
15 changes: 15 additions & 0 deletions NotaionWebApp/Notaion.Infrastructure/Options/CloudinaryOptions.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.Infrastructure.Options
{
public class CloudinaryOptions
{
public string CloudName { get; set; }
public string ApiKey { get; set; }
public string ApiSecret { get; set; }
}
}
41 changes: 41 additions & 0 deletions NotaionWebApp/Notaion.Infrastructure/Persistence/ChatRepository.cs
Original file line number Diff line number Diff line change
@@ -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<GetChatRequest> GetChats()
{
return this._context.Chat
.OrderBy(c => c.SentDate)
.Where(x => x.Hide == false)
.ToList()
.Select(p => this._mapper.Map<GetChatRequest>(p)).ToList();
}

public List<GetChatRequest> GetChatsHide()
{
return this._context.Chat
.OrderBy(c => c.SentDate)
.Where(x => x.Hide == true)
.ToList()
.Select(p => this._mapper.Map<GetChatRequest>(p)).ToList();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string> UploadImageAsync(IFormFile imageFile);
}

public class CloudinaryService : ICloudinaryService
{
private readonly Cloudinary _cloudinary;

public CloudinaryService(Cloudinary cloudinary)
public CloudinaryService(IOptions<CloudinaryOptions> options)
{
_cloudinary = cloudinary;
var account = new Account(
options.Value.CloudName,
options.Value.ApiKey,
options.Value.ApiSecret
);
_cloudinary = new Cloudinary(account);
}

[RequestSizeLimit(1024 * 1024 * 100)]
Expand All @@ -44,7 +48,7 @@ public async Task<string> UploadImageAsync(IFormFile imageFile)

if (uploadResult.StatusCode == System.Net.HttpStatusCode.OK)
{
return uploadResult.SecureUri.AbsoluteUri;
return uploadResult.SecureUrl.AbsoluteUri;
}
else
{
Expand Down
9 changes: 0 additions & 9 deletions NotaionWebApp/Notaion/Configurations/CloudinarySetting.cs

This file was deleted.

38 changes: 8 additions & 30 deletions NotaionWebApp/Notaion/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -20,49 +22,25 @@ public class ChatController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly IHubContext<ChatHub> _hubContext;
private readonly IChatRepository chatRepository;

public ChatController(ApplicationDbContext context, IHubContext<ChatHub> hubContext)
public ChatController(ApplicationDbContext context, IHubContext<ChatHub> 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")]
Expand Down
3 changes: 1 addition & 2 deletions NotaionWebApp/Notaion/Controllers/ItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading

0 comments on commit fb80c4e

Please sign in to comment.