// PUT: api/RentalAgency/UpdateBranch public async Task <IActionResult> UpdateBranch([FromBody] UpdateBranchModel branchModel) { RentalAgencyBranch branch = await dbContext.RentalAgencyBranches .Include(rab => rab.Location) .FirstOrDefaultAsync(rab => rab.Id == branchModel.Id); if (branch == null) { return(NotFound()); } branch.NearAirpot = branchModel.NearAirport == "true" ? true : false; branch.Location.Adress = branchModel.Address; branch.Location.City = branchModel.City; branch.Location.Country = branchModel.Country; if (!TimeComparator(branchModel.WorksFrom, branch.WorkTimeFrom)) { branch.WorkTimeFrom = String2Time(branchModel.WorksFrom); } if (!TimeComparator(branchModel.WorksTo, branch.WorkTimeTo)) { branch.WorkTimeTo = String2Time(branchModel.WorksTo); } dbContext.RentalAgencyBranches.Update(branch); dbContext.SaveChanges(); return(NoContent()); }
private async Task <TResponse <bool> > CanUpdate(UpdateBranchModel request) { try { var branch = await _branchCacheService.GetByName(request.Name); if (branch != null && branch.Id != request.Id) { return(await Fail <bool>(ErrorEnum.BRANCH_HAS_EXIST.GetStringValue())); } return(await Ok(true)); } catch (Exception exception) { return(await Fail <bool>(exception)); } }
public async Task <TResponse <bool> > Update(int userId, UpdateBranchModel request, int permissionId) { try { var checkValid = await _userService.CheckPermission(userId, permissionId); if (checkValid.IsSuccess) { var canUpdate = await CanUpdate(request); if (canUpdate.IsSuccess) { var result = await WriteRepository.ExecuteAsync(SqlQuery.BRANCH_UPDATE, new { request.Id, request.Name, request.Hotline, request.Address, request.ProvinceId, request.DistrictId, request.WardId, UserUpdated = userId, DateUpdated = DateTime.Now }); if (result.IsSuccess) { if (result.Data > 0) { #region Update redis cache await _branchCacheService.AddOrUpdate(new BranchCacheModel { Id = request.Id, Name = request.Name, Hotline = request.Hotline, ProvinceId = request.ProvinceId, DistrictId = request.DistrictId, WardId = request.WardId, Address = request.Address }); #endregion return(await Ok(true)); } return(await Fail <bool>(ErrorEnum.BAD_REQUEST.GetStringValue())); } return(await Fail <bool>(result.Message)); } return(await Fail <bool>(canUpdate.Message)); } return(await Fail <bool>(checkValid.Message)); } catch (Exception exception) { return(await Fail <bool>(exception)); } }
public async Task <ActionResult <bool> > Update(UpdateBranchModel request) { return(Ok(await _branchService.Update(await GetUserId(), request, GetPermissionId()))); }