Exemplo n.º 1
0
        public async Task <HuongNghienCuu> UpdateById(int id, HuongNghienCuuForUpdateDto huongNghienCuu)
        {
            var oldRecord = await _context.DanhSachKhoa.AsNoTracking().FirstOrDefaultAsync(x => x.MaKhoa == id);

            var huongNghienCuuToUpdate = new HuongNghienCuu
            {
                MaHuongNghienCuu  = id,
                TenHuongNghienCuu = huongNghienCuu.TenHuongNghienCuu,
                ThoiGianTao       = oldRecord.ThoiGianTao,
                ThoiGianCapNhat   = DateTime.Now,
                TrangThai         = huongNghienCuu.TrangThai
            };

            _context.DanhSachHuongNghienCuu.Update(huongNghienCuuToUpdate);
            await _context.SaveChangesAsync();

            return(huongNghienCuuToUpdate);
        }
        public async Task <IActionResult> UpdateById(int id, HuongNghienCuuForUpdateDto huongNghienCuu)
        {
            try
            {
                var validationResult = _repo.ValidateBeforeUpdate(id, huongNghienCuu);

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

                    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
                    }
                }));
            }
        }
Exemplo n.º 3
0
        public ValidationResultDto ValidateBeforeUpdate(int id, HuongNghienCuuForUpdateDto huongNghienCuu)
        {
            var totalTenHuongNghienCuu = _context.DanhSachHuongNghienCuu.Count(x => x.MaHuongNghienCuu != id && x.TenHuongNghienCuu.ToLower().Contains(huongNghienCuu.TenHuongNghienCuu.ToLower()));

            IDictionary <string, string[]> Errors = new Dictionary <string, string[]>();

            if (totalTenHuongNghienCuu > 0)
            {
                Errors.Add("tenHuongNghienCuu", new string[] { "tenHuongNghienCuu is duplicated!" });
                return(new ValidationResultDto
                {
                    IsValid = false,
                    Errors = Errors
                });
            }
            else
            {
                return(new ValidationResultDto
                {
                    IsValid = true
                });
            }
        }