public async Task <TaiSanThietBi> Create(TaiSanThietBiForCreateDto taiSanThietBi) { var newTaiSanThietBi = new TaiSanThietBi { MaTSTB = GenerateId(), MaNhaCungCap = taiSanThietBi.MaNhaCungCap, TenTSTB = taiSanThietBi.TenTSTB, TinhTrang = taiSanThietBi.TinhTrang, ThongTinBaoHanh = taiSanThietBi.ThongTinBaoHanh, ThoiGianCapNhat = DateTime.Now, ThoiGianTao = DateTime.Now, TrangThai = 1 }; await _context.DanhSachTaiSanThietBi.AddAsync(newTaiSanThietBi); await _context.SaveChangesAsync(); return(newTaiSanThietBi); }
public async Task <IActionResult> Create(TaiSanThietBiForCreateDto taiSanThietBi) { try { var validationResult = _repo.ValidateBeforeCreate(taiSanThietBi); if (validationResult.IsValid) { var result = await _repo.Create(taiSanThietBi); return(StatusCode(201, new SuccessResponseDto { Message = "Tạo " + _entityName + " mới thành công!", Result = new SuccessResponseResultWithSingleDataDto { Data = result } })); } else { return(StatusCode(500, new FailedResponseDto { Message = "Tạo " + _entityName + " mới thất bại!", Result = new FailedResponseResultDto { Errors = validationResult.Errors } })); } } catch (Exception e) { return(StatusCode(500, new FailedResponseDto { Message = "Tạo " + _entityName + " mới thất bại!", Result = new FailedResponseResultDto { Errors = e } })); } }
public ValidationResultDto ValidateBeforeCreate(TaiSanThietBiForCreateDto taiSanThietBi) { var totalTSTB = _context.DanhSachTaiSanThietBi.Count(x => x.TenTSTB.ToLower() == taiSanThietBi.TenTSTB.ToLower()); IDictionary <string, string[]> Errors = new Dictionary <string, string[]>(); if (totalTSTB >= 1) { Errors.Add("tenTSTB", new string[] { "tenTSTB is duplicated!" }); return(new ValidationResultDto { IsValid = false, Errors = Errors }); } else { return(new ValidationResultDto { IsValid = true }); } }