public ValidationResultDto ValidateBeforeCreate(QuyDinhForCreateDto quyDinh) { var totalTenQuyDinh = _context.DanhSachQuyDinh.Count(x => x.TenQuyDinh.ToLower() == quyDinh.TenQuyDinh.ToLower()); var totalThoiGianBatDauHieuLuc = _context.DanhSachQuyDinh.Count(x => x.ThoiGianBatDauHieuLuc == quyDinh.ThoiGianBatDauHieuLuc); IDictionary <string, string[]> Errors = new Dictionary <string, string[]>(); if (totalTenQuyDinh >= 1 || totalThoiGianBatDauHieuLuc >= 1) { if (totalTenQuyDinh >= 1) { Errors.Add("tenQuyDinh", new string[] { "tenQuyDinh is duplicated!" }); } if (totalThoiGianBatDauHieuLuc >= 1) { Errors.Add("thoiGianBatDauHieuLuc", new string[] { "thoiGianBatDauHieuLuc is duplicated!" }); } return(new ValidationResultDto { IsValid = false, Errors = Errors }); } else { return(new ValidationResultDto { IsValid = true }); } }
public async Task <QuyDinh> Create(QuyDinhForCreateDto quyDinh) { var danhSachQuyDinh = await _context.DanhSachQuyDinh.OrderByDescending(x => x.MaQuyDinh).FirstOrDefaultAsync(); var maQuyDinh = 0; if (danhSachQuyDinh == null) { maQuyDinh = 0; } else { maQuyDinh = danhSachQuyDinh.MaQuyDinh + 1; } var newQuyDinh = new QuyDinh(); newQuyDinh.MaQuyDinh = maQuyDinh; newQuyDinh.TenQuyDinh = quyDinh.TenQuyDinh; newQuyDinh.ThoiGianBatDauHieuLuc = quyDinh.ThoiGianBatDauHieuLuc; newQuyDinh.SoSVTHToiThieu = quyDinh.SoSVTHToiThieu; newQuyDinh.SoSVTHToiDa = quyDinh.SoSVTHToiDa; newQuyDinh.SoGVHDToiThieu = quyDinh.SoGVHDToiThieu; newQuyDinh.SoGVHDToiDa = quyDinh.SoGVHDToiDa; newQuyDinh.SoGVPBToiThieu = quyDinh.SoGVPBToiThieu; newQuyDinh.SoGVPBToiDa = quyDinh.SoGVPBToiDa; newQuyDinh.SoTVHDToiThieu = quyDinh.SoTVHDToiThieu; newQuyDinh.SoTVHDToiDa = quyDinh.SoTVHDToiDa; newQuyDinh.SoCTHDToiThieu = quyDinh.SoCTHDToiThieu; newQuyDinh.SoCTHDToiDa = quyDinh.SoCTHDToiDa; newQuyDinh.SoTKHDToiThieu = quyDinh.SoTKHDToiThieu; newQuyDinh.SoTKHDToiDa = quyDinh.SoTKHDToiDa; newQuyDinh.SoUVHDToiThieu = quyDinh.SoUVHDToiThieu; newQuyDinh.SoUVHDToiDa = quyDinh.SoUVHDToiDa; newQuyDinh.SoChuSoThapPhan = quyDinh.SoChuSoThapPhan; newQuyDinh.DiemSoToiThieu = quyDinh.DiemSoToiThieu; newQuyDinh.DiemSoToiDa = quyDinh.DiemSoToiDa; newQuyDinh.HeSoGVHD = quyDinh.HeSoGVHD; newQuyDinh.HeSoGVPB = quyDinh.HeSoGVPB; newQuyDinh.HeSoTVHD = quyDinh.HeSoTVHD; newQuyDinh.ThoiGianTao = DateTime.Now; newQuyDinh.ThoiGianCapNhat = DateTime.Now; newQuyDinh.TrangThai = 1; await _context.DanhSachQuyDinh.AddAsync(newQuyDinh); await _context.SaveChangesAsync(); return(newQuyDinh); }
public async Task <IActionResult> Create(QuyDinhForCreateDto quyDinh) { try { var validationResult = _repo.ValidateBeforeCreate(quyDinh); if (validationResult.IsValid) { var result = await _repo.Create(quyDinh); 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 } })); } }