Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Contracts.RequestModels.Customer
{
public class CustomerDataListRequest : IRequest<CustomerDataListResponse>
{

}
}
24 changes: 24 additions & 0 deletions Contracts/RequestModels/Customer/UpdateCustomerDataRequest.cs
Original file line number Diff line number Diff line change
@@ -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<UpdateCustomerDataResponse>
{
public Guid? CustomerId { get; set; }


}

public class UpdateCustomerDataModel
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;

}
}
10 changes: 10 additions & 0 deletions Contracts/ResponseModels/Customer/UpdateCustomerDataResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace Contracts.ResponseModels.Customer
{
public class UpdateCustomerDataResponse
{
public bool Success { get; set; }
public string Message { get; set; } = string.Empty;
}

}
Original file line number Diff line number Diff line change
@@ -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<UpdateCustomerDataRequest, UpdateCustomerDataResponse>
{
private readonly DBContext _db;
public UpdateCustomerDataHandler(DBContext db)
{
_db = db;
}
public async Task<UpdateCustomerDataResponse> 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."
};

}

}
}
35 changes: 35 additions & 0 deletions Services/Validators/Customer/UpdateCustomerValidator.cs
Original file line number Diff line number Diff line change
@@ -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<UpdateCustomerDataRequest>
{
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 <bool> AvailableEmails (string email, CancellationToken cancellationToken)
{
var existingEmail = await _db.Customers.Where(Q => Q.Email == email).AsNoTracking().AnyAsync();

return !existingEmail;
}

}
}
16 changes: 15 additions & 1 deletion WebApiTraining2/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -56,14 +57,27 @@ public async Task<ActionResult<CreateCustomerResponse>> Post([FromBody] CreateCu

// PUT api/<CustomerController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
public async Task <ActionResult<UpdateCustomerDataRequest>> Put(Guid id, [FromBody] UpdateCustomerDataModel model, [FromServices] IValidator<UpdateCustomerDataRequest> 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/<CustomerController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{

}
}
}
Binary file modified WebApiTraining2/training.db
Binary file not shown.
Binary file added WebApiTraining2/training.db-shm
Binary file not shown.
Empty file added WebApiTraining2/training.db-wal
Empty file.