public async Task <IActionResult> UpdateById(string id, TaiSanThietBiForUpdateDto taiSanThietBi) { try { var validationResult = _repo.ValidateBeforeUpdate(id, taiSanThietBi); if (validationResult.IsValid) { var result = await _repo.UpdateById(id, taiSanThietBi); return(StatusCode(200, new SuccessResponseDto { Message = "Cập nhật " + _entityName + " thành công!", Result = new SuccessResponseResultWithSingleDataDto { Data = result } })); } else { return(StatusCode(500, new FailedResponseDto { Message = "Cập nhật " + _entityName + " mới thất bại!", Result = new FailedResponseResultDto { Errors = validationResult.Errors } })); } } catch (Exception e) { return(StatusCode(500, new FailedResponseDto { Message = "Cập nhật " + _entityName + " thất bại!", Result = new FailedResponseResultDto { Errors = e } })); } }
public async Task <TaiSanThietBi> UpdateById(string id, TaiSanThietBiForUpdateDto taiSanThietBi) { var oldRecord = await _context.DanhSachTaiSanThietBi.AsNoTracking().FirstOrDefaultAsync(x => x.MaTSTB == id); var TSTBToUpdate = new TaiSanThietBi { MaTSTB = id, MaNhaCungCap = taiSanThietBi.MaNhaCungCap, TenTSTB = taiSanThietBi.TenTSTB, TinhTrang = taiSanThietBi.TinhTrang, ThongTinBaoHanh = taiSanThietBi.ThongTinBaoHanh, ThoiGianCapNhat = DateTime.Now, ThoiGianTao = DateTime.Now, TrangThai = 1 }; _context.DanhSachTaiSanThietBi.Update(TSTBToUpdate); await _context.SaveChangesAsync(); return(TSTBToUpdate); }
public ValidationResultDto ValidateBeforeUpdate(string id, TaiSanThietBiForUpdateDto taiSanThietBi) { var totalTSTB = _context.DanhSachTaiSanThietBi.Count(x => x.MaTSTB != id && x.TenTSTB.ToLower() == taiSanThietBi.TenTSTB.ToLower()); IDictionary <string, string[]> Errors = new Dictionary <string, string[]>(); if (totalTSTB > 0) { Errors.Add("tenTSTB", new string[] { "tenTSTB is duplicated!" }); return(new ValidationResultDto { IsValid = false, Errors = Errors }); } else { return(new ValidationResultDto { IsValid = true }); } }