Ejemplo n.º 1
0
        public async Task <IActionResult> Create(DichVuForCreateDto dichVu)
        {
            try
            {
                var validationResult = _repo.ValidateBeforeCreate(dichVu);

                if (validationResult.IsValid)
                {
                    var result = await _repo.Create(dichVu);

                    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
                    }
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task <DichVu> Create(DichVuForCreateDto dichVu)
        {
            var newDichVu = new DichVu
            {
                MaDichVu        = GenerateId(),
                TenDichVu       = dichVu.TenDichVu,
                DonGia          = dichVu.DonGia,
                DVT             = dichVu.DVT,
                GhiChu          = dichVu.GhiChu,
                ThoiGianTao     = DateTime.Now,
                ThoiGianCapNhat = DateTime.Now,
                TrangThai       = 1
            };

            await _context.DanhSachDichVu.AddAsync(newDichVu);

            await _context.SaveChangesAsync();

            return(newDichVu);
        }
Ejemplo n.º 3
0
        public ValidationResultDto ValidateBeforeCreate(DichVuForCreateDto dichVu)
        {
            var totalTenDichVu = _context.DanhSachDichVu.Count(x => x.TenDichVu == dichVu.TenDichVu);
            IDictionary <string, string[]> Errors = new Dictionary <string, string[]>();

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

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