Beispiel #1
0
        public async Task <IActionResult> UpdateById(string id, DichVuForUpdateDto dichVu)
        {
            try
            {
                var validationResult = _repo.ValidateBeforeUpdate(id, dichVu);

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

                    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 #2
0
        public async Task <DichVu> UpdateById(string id, DichVuForUpdateDto dichVu)
        {
            var oldRecord = await _context.DanhSachDichVu.AsNoTracking().FirstOrDefaultAsync(x => x.MaDichVu == id);

            var dichVuToUpdate = new DichVu
            {
                MaDichVu        = id,
                TenDichVu       = dichVu.TenDichVu,
                DonGia          = dichVu.DonGia,
                DVT             = dichVu.DVT,
                GhiChu          = dichVu.GhiChu,
                TrangThai       = dichVu.TrangThai,
                ThoiGianTao     = oldRecord.ThoiGianTao,
                ThoiGianCapNhat = DateTime.Now
            };

            _context.DanhSachDichVu.Update(dichVuToUpdate);
            await _context.SaveChangesAsync();

            return(dichVuToUpdate);
        }
Beispiel #3
0
        public ValidationResultDto ValidateBeforeUpdate(string id, DichVuForUpdateDto dichVu)
        {
            var totalTenDichVu = _context.DanhSachDichVu.Count(x => x.MaDichVu != id && x.TenDichVu == dichVu.TenDichVu);
            IDictionary <string, string[]> Errors = new Dictionary <string, string[]>();

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

                return(new ValidationResultDto
                {
                    IsValid = false,
                    Errors = Errors
                });
            }
            else
            {
                return(new ValidationResultDto
                {
                    IsValid = true
                });
            }
        }