diff --git a/Contracts/RequestModels/Customer/CustomerDataListRequest.cs b/Contracts/RequestModels/Customer/CustomerDataListRequest.cs index 8dec991..af44ac7 100644 --- a/Contracts/RequestModels/Customer/CustomerDataListRequest.cs +++ b/Contracts/RequestModels/Customer/CustomerDataListRequest.cs @@ -5,5 +5,6 @@ namespace Contracts.RequestModels.Customer { public class CustomerDataListRequest : IRequest { + } } diff --git a/Contracts/RequestModels/Customer/UpdateCustomerDataRequest.cs b/Contracts/RequestModels/Customer/UpdateCustomerDataRequest.cs new file mode 100644 index 0000000..ce1d8cb --- /dev/null +++ b/Contracts/RequestModels/Customer/UpdateCustomerDataRequest.cs @@ -0,0 +1,24 @@ +using Contracts.ResponseModels.Customer; +using MediatR; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.RequestModels.Customer +{ + public class UpdateCustomerDataRequest : UpdateCustomerDataModel, IRequest + { + public Guid? CustomerId { get; set; } + + + } + + public class UpdateCustomerDataModel + { + public string Name { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + + } +} diff --git a/Contracts/ResponseModels/Customer/UpdateCustomerDataResponse.cs b/Contracts/ResponseModels/Customer/UpdateCustomerDataResponse.cs new file mode 100644 index 0000000..8407a43 --- /dev/null +++ b/Contracts/ResponseModels/Customer/UpdateCustomerDataResponse.cs @@ -0,0 +1,10 @@ + +namespace Contracts.ResponseModels.Customer +{ + public class UpdateCustomerDataResponse + { + public bool Success { get; set; } + public string Message { get; set; } = string.Empty; + } + +} diff --git a/Services/RequestHandlers/ManageCustomer/UpdateCustomerDataHandler.cs b/Services/RequestHandlers/ManageCustomer/UpdateCustomerDataHandler.cs new file mode 100644 index 0000000..1017876 --- /dev/null +++ b/Services/RequestHandlers/ManageCustomer/UpdateCustomerDataHandler.cs @@ -0,0 +1,42 @@ + +using Contracts.RequestModels.Customer; +using Contracts.ResponseModels.Customer; +using Entity.Entity; +using MediatR; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage.Json; + +namespace Services.RequestHandlers.ManageCustomer +{ + public class UpdateCustomerDataHandler : IRequestHandler + { + private readonly DBContext _db; + public UpdateCustomerDataHandler(DBContext db) + { + _db = db; + } + public async Task Handle(UpdateCustomerDataRequest request, CancellationToken cancellationToken) + { + var existingData = await _db.Customers.FindAsync(request.CustomerId); + if (existingData == null) + { + return new UpdateCustomerDataResponse() + { + Success = false, + Message = "Data Tidak ditemukan" + }; + } + existingData.Name = request.Name; + existingData.Email = request.Email; + _db.Customers.Update(existingData); + await _db.SaveChangesAsync(cancellationToken); + return new UpdateCustomerDataResponse() + { + Success = true, + Message = "Data Updated." + }; + + } + + } +} diff --git a/Services/Validators/Customer/UpdateCustomerValidator.cs b/Services/Validators/Customer/UpdateCustomerValidator.cs new file mode 100644 index 0000000..40019d4 --- /dev/null +++ b/Services/Validators/Customer/UpdateCustomerValidator.cs @@ -0,0 +1,35 @@ +using Contracts.RequestModels.Customer; +using Entity.Entity; +using FluentValidation; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Services.Validators.Customer +{ + public class UpdateCustomerValidator : AbstractValidator + { + private readonly DBContext _db; + public UpdateCustomerValidator(DBContext db) + { + _db = db; + RuleFor(Q => Q.Name).NotEmpty().WithMessage("Name Can't be Empty!") + .MaximumLength(20).WithMessage("Maximum Name's Length 20 Characters!"); + + RuleFor(Q => Q.Email).EmailAddress().NotEmpty().WithMessage("Email Can't Be Empty!") + .MustAsync(AvailableEmails).WithMessage("The email you give is already exist!"); + + } + + public async Task AvailableEmails (string email, CancellationToken cancellationToken) + { + var existingEmail = await _db.Customers.Where(Q => Q.Email == email).AsNoTracking().AnyAsync(); + + return !existingEmail; + } + + } +} diff --git a/WebApiTraining2/Controllers/CustomerController.cs b/WebApiTraining2/Controllers/CustomerController.cs index 942bc74..c353f8c 100644 --- a/WebApiTraining2/Controllers/CustomerController.cs +++ b/WebApiTraining2/Controllers/CustomerController.cs @@ -4,6 +4,7 @@ using FluentValidation; using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Mvc; +using Services.Validators.Customer; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -56,14 +57,27 @@ public async Task> Post([FromBody] CreateCu // PUT api//5 [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) + public async Task > Put(Guid id, [FromBody] UpdateCustomerDataModel model, [FromServices] IValidator validator, CancellationToken cancellationToken) { + var request = new UpdateCustomerDataRequest { CustomerId = id, Name = model.Name, Email = model.Email }; + //request.CustomerId = id; + var validationResult = await validator.ValidateAsync(request); + if (!validationResult.IsValid) + { + validationResult.AddToModelState(ModelState); + return ValidationProblem(ModelState); + } + + var response = await _mediator.Send(request, cancellationToken); + return Ok(response); + } // DELETE api//5 [HttpDelete("{id}")] public void Delete(int id) { + } } } diff --git a/WebApiTraining2/training.db b/WebApiTraining2/training.db index afe972d..13e353a 100644 Binary files a/WebApiTraining2/training.db and b/WebApiTraining2/training.db differ diff --git a/WebApiTraining2/training.db-shm b/WebApiTraining2/training.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/WebApiTraining2/training.db-shm differ diff --git a/WebApiTraining2/training.db-wal b/WebApiTraining2/training.db-wal new file mode 100644 index 0000000..e69de29