From 8561f7695d2dc81a01f7b4855f542291e87a3a9b Mon Sep 17 00:00:00 2001 From: Eric <36512385+PhucNghi176@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:15:40 +0700 Subject: [PATCH] Update --- .../Extensions/CurrentUserService.cs | 1 + .../Feedbacks/CreateFeedbackCommandHandler.cs | 35 ++++++++++++++++++ .../Services/Feedbacks/Command.cs | 8 ++++ .../Repositories/ICurrentUserService.cs | 1 + MBS_COMMAND.Domain/Entities/Feedback.cs | 14 ++++--- MBS_COMMAND.Domain/Entities/Schedule.cs | 3 +- .../ApplicationDbContextModelSnapshot.cs | 37 ++++++++++++++++--- .../APIs/Feedbacks/FeedbackApi.cs | 21 +++++++++++ 8 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 MBS_COMMAND.Application/UserCases/Commands/Feedbacks/CreateFeedbackCommandHandler.cs create mode 100644 MBS_COMMAND.Contract/Services/Feedbacks/Command.cs create mode 100644 MBS_COMMAND.Presentation/APIs/Feedbacks/FeedbackApi.cs diff --git a/MBS-COMMAND.API/DependencyInjection/Extensions/CurrentUserService.cs b/MBS-COMMAND.API/DependencyInjection/Extensions/CurrentUserService.cs index e1dde5a..8d0b705 100644 --- a/MBS-COMMAND.API/DependencyInjection/Extensions/CurrentUserService.cs +++ b/MBS-COMMAND.API/DependencyInjection/Extensions/CurrentUserService.cs @@ -8,6 +8,7 @@ public class CurrentUserService(IHttpContextAccessor httpContextAccessor) : ICur private readonly ClaimsPrincipal? _claimsPrincipal = httpContextAccessor?.HttpContext?.User; public string? UserId => _claimsPrincipal?.FindFirstValue("UserId"); + public string? Role => _claimsPrincipal?.FindFirstValue(ClaimTypes.Role); public string? UserName => _claimsPrincipal?.FindFirstValue(ClaimTypes.Name); diff --git a/MBS_COMMAND.Application/UserCases/Commands/Feedbacks/CreateFeedbackCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Feedbacks/CreateFeedbackCommandHandler.cs new file mode 100644 index 0000000..67c94ad --- /dev/null +++ b/MBS_COMMAND.Application/UserCases/Commands/Feedbacks/CreateFeedbackCommandHandler.cs @@ -0,0 +1,35 @@ +using MBS_COMMAND.Contract.Abstractions.Messages; +using MBS_COMMAND.Contract.Abstractions.Shared; +using MBS_COMMAND.Contract.Services.Feedbacks; +using MBS_COMMAND.Domain.Abstractions.Repositories; +using MBS_COMMAND.Domain.Entities; + +namespace MBS_COMMAND.Application.UserCases.Commands.Feedbacks; + +public class CreateFeedbackCommandHandler : ICommandHandler +{ + private readonly IRepositoryBase _feedbackRepository; + private readonly ICurrentUserService _currentUserService; + + public CreateFeedbackCommandHandler(IRepositoryBase feedbackRepository, + ICurrentUserService currentUserService) + { + _feedbackRepository = feedbackRepository; + _currentUserService = currentUserService; + } + + public async Task Handle(Command.CreateFeedback request, CancellationToken cancellationToken) + { + var role = _currentUserService.Role == "2"; + var feedback = new Feedback + { + Content = request.Content, + GroupId = request.GroupId, + Rating = request.Rating, + ScheduleId = request.ScheduleId, + IsMentor = role, + }; + _feedbackRepository.Add(feedback); + return Result.Success(); + } +} \ No newline at end of file diff --git a/MBS_COMMAND.Contract/Services/Feedbacks/Command.cs b/MBS_COMMAND.Contract/Services/Feedbacks/Command.cs new file mode 100644 index 0000000..10eaed6 --- /dev/null +++ b/MBS_COMMAND.Contract/Services/Feedbacks/Command.cs @@ -0,0 +1,8 @@ +using MBS_COMMAND.Contract.Abstractions.Messages; + +namespace MBS_COMMAND.Contract.Services.Feedbacks; + +public static class Command +{ + public record CreateFeedback(Guid GroupId, string? Content,Guid ScheduleId, int Rating) : ICommand; +} \ No newline at end of file diff --git a/MBS_COMMAND.Domain/Abstractions/Repositories/ICurrentUserService.cs b/MBS_COMMAND.Domain/Abstractions/Repositories/ICurrentUserService.cs index 4233de1..9e4db38 100644 --- a/MBS_COMMAND.Domain/Abstractions/Repositories/ICurrentUserService.cs +++ b/MBS_COMMAND.Domain/Abstractions/Repositories/ICurrentUserService.cs @@ -4,4 +4,5 @@ public interface ICurrentUserService { string? UserId { get; } string? UserName { get; } + string? Role { get; } } diff --git a/MBS_COMMAND.Domain/Entities/Feedback.cs b/MBS_COMMAND.Domain/Entities/Feedback.cs index 49dd02c..fc2d2ee 100644 --- a/MBS_COMMAND.Domain/Entities/Feedback.cs +++ b/MBS_COMMAND.Domain/Entities/Feedback.cs @@ -1,15 +1,19 @@ -using MBS_COMMAND.Domain.Abstractions.Entities; +using System.ComponentModel.DataAnnotations; +using MBS_COMMAND.Domain.Abstractions.Entities; namespace MBS_COMMAND.Domain.Entities; public class Feedback : Entity, IAuditableEntity { - + [MaxLength(100)] public string? Content { get; set; } + [Range(1,5)] public int Rating { get; set; } - public Guid? SlotId { get; set; } - public virtual Slot? Slot { get; set; } + public Guid? ScheduleId { get; set; } + public virtual Schedule? Schedule { get; set; } + public Guid? GroupId { get; set; } + public virtual Group? Group { get; set; } public bool IsMentor { get; set; } public DateTimeOffset CreatedOnUtc { get; set; } public DateTimeOffset? ModifiedOnUtc { get; set; } -} +} \ No newline at end of file diff --git a/MBS_COMMAND.Domain/Entities/Schedule.cs b/MBS_COMMAND.Domain/Entities/Schedule.cs index be1bd1f..068c15e 100644 --- a/MBS_COMMAND.Domain/Entities/Schedule.cs +++ b/MBS_COMMAND.Domain/Entities/Schedule.cs @@ -6,7 +6,8 @@ public class Schedule : Entity, IAuditableEntity { public Guid MentorId { get; set; } public virtual User? Mentor { get; set; } - + public Guid SlotId { get; set; } + public virtual Slot? Slot { get; set; } public Guid GroupId { get; set; } public virtual Group? Group { get; set; } public TimeOnly StartTime { get; set; } diff --git a/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs b/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs index 2201192..d3577f7 100644 --- a/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs @@ -106,11 +106,15 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("uniqueidentifier"); b.Property("Content") - .HasColumnType("nvarchar(max)"); + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); b.Property("CreatedOnUtc") .HasColumnType("datetimeoffset"); + b.Property("GroupId") + .HasColumnType("uniqueidentifier"); + b.Property("IsDeleted") .HasColumnType("bit"); @@ -123,12 +127,14 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("Rating") .HasColumnType("int"); - b.Property("SlotId") + b.Property("ScheduleId") .HasColumnType("uniqueidentifier"); b.HasKey("Id"); - b.HasIndex("SlotId"); + b.HasIndex("GroupId"); + + b.HasIndex("ScheduleId"); b.ToTable("Feedbacks"); }); @@ -279,6 +285,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("ModifiedOnUtc") .HasColumnType("datetimeoffset"); + b.Property("SlotId") + .HasColumnType("uniqueidentifier"); + b.Property("StartTime") .HasColumnType("time"); @@ -291,6 +300,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("MentorId"); + b.HasIndex("SlotId"); + b.HasIndex("SubjectId"); b.ToTable("Schedules"); @@ -565,11 +576,17 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("MBS_COMMAND.Domain.Entities.Feedback", b => { - b.HasOne("MBS_COMMAND.Domain.Entities.Slot", "Slot") + b.HasOne("MBS_COMMAND.Domain.Entities.Group", "Group") .WithMany() - .HasForeignKey("SlotId"); + .HasForeignKey("GroupId"); - b.Navigation("Slot"); + b.HasOne("MBS_COMMAND.Domain.Entities.Schedule", "Schedule") + .WithMany() + .HasForeignKey("ScheduleId"); + + b.Navigation("Group"); + + b.Navigation("Schedule"); }); modelBuilder.Entity("MBS_COMMAND.Domain.Entities.Group", b => @@ -645,6 +662,12 @@ protected override void BuildModel(ModelBuilder modelBuilder) .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("MBS_COMMAND.Domain.Entities.Slot", "Slot") + .WithMany() + .HasForeignKey("SlotId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("MBS_COMMAND.Domain.Entities.Subject", "Subject") .WithMany() .HasForeignKey("SubjectId") @@ -655,6 +678,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("Mentor"); + b.Navigation("Slot"); + b.Navigation("Subject"); }); diff --git a/MBS_COMMAND.Presentation/APIs/Feedbacks/FeedbackApi.cs b/MBS_COMMAND.Presentation/APIs/Feedbacks/FeedbackApi.cs new file mode 100644 index 0000000..54c740f --- /dev/null +++ b/MBS_COMMAND.Presentation/APIs/Feedbacks/FeedbackApi.cs @@ -0,0 +1,21 @@ +using MBS_COMMAND.Contract.Services.Feedbacks; +using MBS_COMMAND.Presentation.Abstractions; + +namespace MBS_COMMAND.Presentation.APIs.Feedbacks; + +public class FeedbackApi : ApiEndpoint,ICarterModule +{ + private const string BaseUrl = "api/v{version:apiVersion}/feedbacks"; + public void AddRoutes(IEndpointRouteBuilder app) + { + var gr1 = app.NewVersionedApi("Feedbacks").MapGroup(BaseUrl).HasApiVersion(1); + gr1.MapPost(string.Empty, CreateFeedback).RequireAuthorization(); + + } + + private static async Task CreateFeedback(ISender sender, [FromBody] Command.CreateFeedback request) + { + var result = await sender.Send(request); + return result.IsFailure ? HandlerFailure(result) : Results.Ok(result); + } +} \ No newline at end of file