Handler.cs 904 B

123456789101112131415161718192021222324
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. namespace Application.Features.Api.Note.SendNote;
  4. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Response>
  5. {
  6. public async Task<Response> Handle(Command r, CancellationToken ct)
  7. {
  8. if (string.IsNullOrWhiteSpace(r.Title) || string.IsNullOrWhiteSpace(r.Content))
  9. {
  10. throw new InvalidOperationException("제목과 내용을 입력해주세요.");
  11. }
  12. var note = r.IsSystem
  13. ? Domain.Entities.Notes.Note.CreateSystem(r.ReceiverMemberID, r.Title, r.Content, r.RelatedType, r.RelatedID)
  14. : Domain.Entities.Notes.Note.Create(r.SenderMemberID, r.ReceiverMemberID, r.Title, r.Content, false, r.RelatedType, r.RelatedID);
  15. db.Note.Add(note);
  16. await db.SaveChangesAsync(ct);
  17. return new Response(note.ID);
  18. }
  19. }