public async Task <GiangVien> Create(GiangVienForCreateDto giangVien) { var newGiangVien = new GiangVien(); newGiangVien.MaGiangVien = GenerateId(); newGiangVien.HoVaTen = giangVien.HoVaTen; newGiangVien.GioiTinh = giangVien.GioiTinh; newGiangVien.NgaySinh = giangVien.NgaySinh; newGiangVien.Email = giangVien.Email; newGiangVien.QueQuan = giangVien.QueQuan; newGiangVien.SoDienThoai = giangVien.SoDienThoai; newGiangVien.DiaChi = giangVien.DiaChi; newGiangVien.DonViCongTac = giangVien.DonViCongTac; newGiangVien.HocVi = giangVien.HocVi; newGiangVien.HocHam = giangVien.HocHam; newGiangVien.ThoiGianTao = DateTime.Now; newGiangVien.ThoiGianCapNhat = DateTime.Now; newGiangVien.TrangThai = 1; if (giangVien.MaKhoa.HasValue) { newGiangVien.MaKhoa = giangVien.MaKhoa; } await _context.DanhSachGiangVien.AddAsync(newGiangVien); await _context.SaveChangesAsync(); return(newGiangVien); }
public ValidationResultDto ValidateBeforeCreate(GiangVienForCreateDto giangVien) { var totalEmail = _context.DanhSachGiangVien.Count(x => x.Email.ToLower() == giangVien.Email.ToLower()); var totalSoDienThoai = _context.DanhSachGiangVien.Count(x => x.SoDienThoai.ToLower() == giangVien.SoDienThoai.ToLower()); IDictionary <string, string[]> Errors = new Dictionary <string, string[]>(); if (totalEmail >= 1 || totalSoDienThoai >= 1) { if (totalEmail >= 1) { Errors.Add("email", new string[] { "email is duplicated!" }); } if (totalSoDienThoai >= 1) { Errors.Add("soDienThoai", new string[] { "soDienThoai is duplicated!" }); } return(new ValidationResultDto { IsValid = false, Errors = Errors }); } else { return(new ValidationResultDto { IsValid = true }); } }
public async Task <IActionResult> Create(GiangVienForCreateDto giangVien) { try { var validationResult = _repo.ValidateBeforeCreate(giangVien); if (validationResult.IsValid) { var result = await _repo.Create(giangVien); 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 } })); } }