public async Task <KhoaDaoTao> Create(KhoaDaoTaoForCreateDto khoaDaoTao) { var danhSachKhoaDaoTao = await _context.DanhSachKhoaDaoTao.OrderByDescending(x => x.MaKhoaDaoTao).FirstOrDefaultAsync(); var maKhoaDaoTao = 0; if (danhSachKhoaDaoTao == null) { maKhoaDaoTao = 0; } else { maKhoaDaoTao = danhSachKhoaDaoTao.MaKhoaDaoTao + 1; } var newKhoaDaoTao = new KhoaDaoTao { MaKhoaDaoTao = maKhoaDaoTao, TenKhoaDaoTao = khoaDaoTao.TenKhoaDaoTao, TenVietTat = khoaDaoTao.TenVietTat, ThoiGianBatDau = khoaDaoTao.ThoiGianBatDau, ThoiGianKetThuc = khoaDaoTao.ThoiGianKetThuc, ThoiGianTao = DateTime.Now, ThoiGianCapNhat = DateTime.Now, TrangThai = 1 }; await _context.DanhSachKhoaDaoTao.AddAsync(newKhoaDaoTao); await _context.SaveChangesAsync(); return(newKhoaDaoTao); }
public ValidationResultDto ValidateBeforeCreate(KhoaDaoTaoForCreateDto khoaDaoTao) { var totalTenKhoaDaoTao = _context.DanhSachKhoaDaoTao.Count(x => x.TenKhoaDaoTao.ToLower().Contains(khoaDaoTao.TenKhoaDaoTao.ToLower())); var totalTenVietTat = _context.DanhSachKhoaDaoTao.Count(x => x.TenVietTat.ToLower().Contains(khoaDaoTao.TenVietTat.ToLower())); var totalThoiGianBatDau = _context.DanhSachKhoaDaoTao.Count(x => x.ThoiGianBatDau == khoaDaoTao.ThoiGianBatDau); var totalThoiGianKetThuc = _context.DanhSachKhoaDaoTao.Count(x => x.ThoiGianKetThuc == khoaDaoTao.ThoiGianKetThuc); var isThoiGianValid = khoaDaoTao.ThoiGianKetThuc >= khoaDaoTao.ThoiGianBatDau; IDictionary <string, string[]> Errors = new Dictionary <string, string[]>(); if (totalTenKhoaDaoTao >= 1 || totalTenVietTat >= 1 || totalThoiGianBatDau >= 1 || totalThoiGianKetThuc >= 1 || isThoiGianValid == false) { if (totalTenKhoaDaoTao >= 1) { Errors.Add("tenKhoaDaoTao", new string[] { "tenKhoaDaoTao is duplicated!" }); } if (totalTenVietTat >= 1) { Errors.Add("tenVietTat", new string[] { "tenVietTat is duplicated!" }); } if (totalThoiGianBatDau >= 1) { Errors.Add("thoiGianBatDau", new string[] { "thoiGianBatDau is duplicated!" }); } if (totalThoiGianKetThuc >= 1) { Errors.Add("thoiGianKetThuc", new string[] { "thoiGianKetThuc is duplicated!" }); } if (isThoiGianValid == false) { Errors.Add("thoiGianKetThuc", new string[] { "thoiGianKetThuc must come before thoiGianBatDau!" }); } return(new ValidationResultDto { IsValid = false, Errors = Errors }); } else { return(new ValidationResultDto { IsValid = true }); } }
public async Task <IActionResult> Create(KhoaDaoTaoForCreateDto khoaDaoTao) { try { var validationResult = _repo.ValidateBeforeCreate(khoaDaoTao); if (validationResult.IsValid) { var result = await _repo.Create(khoaDaoTao); 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 } })); } }