Beispiel #1
0
        public ValidationResultDto ValidateBeforeUpdate(int id, HocKyForUpdateDto hocKy)
        {
            var totalTenHocKy        = _context.DanhSachHocKy.Count(x => x.MaHocKy != id && x.TenHocKy.ToLower().Contains(hocKy.TenHocKy.ToLower()));
            var totalThoiGianBatDau  = _context.DanhSachHocKy.Count(x => x.MaHocKy != id && x.ThoiGianBatDau == hocKy.ThoiGianBatDau);
            var totalThoiGianKetThuc = _context.DanhSachHocKy.Count(x => x.MaHocKy != id && x.ThoiGianKetThuc == hocKy.ThoiGianKetThuc);
            var isThoiGianValid      = hocKy.ThoiGianKetThuc >= hocKy.ThoiGianBatDau;
            IDictionary <string, string[]> Errors = new Dictionary <string, string[]>();

            if (totalTenHocKy > 0 || totalThoiGianBatDau > 0 || totalThoiGianKetThuc > 0 || isThoiGianValid == false)
            {
                if (totalTenHocKy > 0)
                {
                    Errors.Add("tenHocKy", new string[] { "tenHocKy is duplicated!" });
                }

                if (totalThoiGianBatDau > 0)
                {
                    Errors.Add("thoiGianBatDau", new string[] { "thoiGianBatDau is duplicated!" });
                }

                if (totalThoiGianKetThuc > 0)
                {
                    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
                });
            }
        }
Beispiel #2
0
        public async Task <IActionResult> UpdateById(int id, HocKyForUpdateDto hocKy)
        {
            try
            {
                var validationResult = _repo.ValidateBeforeUpdate(id, hocKy);

                if (validationResult.IsValid)
                {
                    var result = await _repo.UpdateById(id, hocKy);

                    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
                    }
                }));
            }
        }
Beispiel #3
0
        public async Task <HocKy> UpdateById(int id, HocKyForUpdateDto hocKy)
        {
            var oldRecord = await _context.DanhSachHocKy.AsNoTracking().FirstOrDefaultAsync(x => x.MaHocKy == id);

            var hocKyToUpdate = new HocKy
            {
                MaHocKy         = id,
                TenHocKy        = hocKy.TenHocKy,
                NamHoc          = hocKy.NamHoc,
                ThoiGianBatDau  = hocKy.ThoiGianBatDau,
                ThoiGianKetThuc = hocKy.ThoiGianKetThuc,
                ThoiGianTao     = oldRecord.ThoiGianTao,
                ThoiGianCapNhat = DateTime.Now,
                TrangThai       = hocKy.TrangThai
            };

            _context.DanhSachHocKy.Update(hocKyToUpdate);
            await _context.SaveChangesAsync();

            return(hocKyToUpdate);
        }