public async Task <Result <bool> > Handle(UpdateGateCommand command, CancellationToken cancellationToken) { try { var user = await _userRepository.GetUserByEmail(command.ModifiedBy); var adminAccess = await _userGateRepository.CheckAdminAccess(command.Id, user.Id); if (!adminAccess && !user.Role.Name.Equals("Admin")) { return(Result <bool> .AccessDenied("You have no access to modify gate!")); } var gateType = await _gateTypeRepository.GetGateTypeByName(command.GateTypeName); var account = await _accountRepository.GetAccountByName(command.AccountName); var currentGate = await _gateRepository.Get(command.Id); await UpdateUserGates(command.Id, command.ModifiedBy, command.Users); UpdateGateProperties(currentGate, command, gateType, account); var res = await _gateRepository.Update(currentGate); return(Result <bool> .Ok(res)); } catch (Exception e) { return(Result <bool> .Failure(e.Message)); } }
private void UpdateGateProperties(Gate currentGate, UpdateGateCommand request, GateType gateType, Account account) { currentGate.Name = request.Name; currentGate.GateTypeId = gateType != null ? gateType.Id : currentGate.GateTypeId; currentGate.AccountId = account != null ? account.Id : currentGate.AccountId; currentGate.ModifiedBy = request.ModifiedBy; currentGate.MoidifiedAt = DateTime.UtcNow; }
public async Task <IActionResult> UpdateGate([FromBody] UpdateGateCommand command) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } command.ModifiedBy = HttpContext.User.Identity.Name; var result = await _mediator.Send(command); return(StatusCodeResult(result)); }