public async Task <ReturnResponse> UpdateAdministrator(int administratorId, AdministratorToUpdate administrator) { if (administrator == null) { return(new ReturnResponse() { StatusCode = Utils.ObjectNull, StatusMessage = Utils.StatusMessageObjectNull }); } //GET USER THAT MADE THIS REQUEST var userTypeIdClaim = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(a => a.Type == ClaimTypes.NameIdentifier); if (userTypeIdClaim == null) { return(new ReturnResponse() { StatusCode = Utils.UserClaimNotFound, StatusMessage = Utils.StatusMessageUserClaimNotFound }); } var userTypeIdVal = Convert.ToInt32(userTypeIdClaim.Value); if ((administratorId != administrator.AdministratorId) || (administratorId != userTypeIdVal)) { return(new ReturnResponse() { StatusCode = Utils.BadRequest, StatusMessage = Utils.StatusMessageBadRequest }); } var administratorDetails = await _dataContext.Administrator.Where(a => a.AdministratorId == administratorId).FirstOrDefaultAsync(); if (administratorDetails == null) { return(new ReturnResponse() { StatusCode = Utils.NotFound, StatusMessage = Utils.StatusMessageNotFound }); } var administratorToUpdate = _mapper.Map(administrator, administratorDetails); var updateResult = _globalRepository.Update(administratorToUpdate); if (!updateResult) { return(new ReturnResponse() { StatusCode = Utils.NotSucceeded, StatusMessage = Utils.StatusMessageNotSucceeded }); } var saveResult = await _globalRepository.SaveAll(); if (!saveResult.HasValue) { return(new ReturnResponse() { StatusCode = Utils.SaveError, StatusMessage = Utils.StatusMessageSaveError }); } if (!saveResult.Value) { return(new ReturnResponse() { StatusCode = Utils.SaveNoRowAffected, StatusMessage = Utils.StatusMessageSaveNoRowAffected }); } return(new ReturnResponse() { StatusCode = Utils.Success, StatusMessage = Utils.StatusMessageSuccess, ObjectValue = administratorToUpdate }); }
public async Task <ActionResult <ReturnResponse> > PutAdministrator([FromRoute] int administratorId, [FromBody] AdministratorToUpdate administratorToUpdate) { var dbTransaction = await _dataContext.Database.BeginTransactionAsync(); var result = await _administratorRepository.UpdateAdministrator(administratorId, administratorToUpdate); if (result.StatusCode == Utils.Success) { result.ObjectValue = _mapper.Map <AdministratorResponse>((Administrator)result.ObjectValue); await dbTransaction.CommitAsync(); return(StatusCode(StatusCodes.Status200OK, result.ObjectValue)); } else { await dbTransaction.RollbackAsync(); return(StatusCode(StatusCodes.Status400BadRequest, result)); } }