private async Task <TResponse <bool> > CanInsert(InsertCustomerVipModel request)
        {
            try
            {
                var existName = await _customerVipCacheService.GetByName(request.Name);

                if (existName != null)
                {
                    return(await Fail <bool>(ErrorEnum.CUSTOMER_VIP_HAS_EXIST.GetStringValue()));
                }

                var existProfit = await _customerVipCacheService.GetByProfit(request.Profit);

                if (existProfit != null)
                {
                    return(await Fail <bool>(ErrorEnum.CUSTOMER_VIP_PROFIT_HAS_EXIST.GetStringValue()));
                }

                return(await Ok(true));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Add(InsertCustomerVipModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _customerVipService.Insert(_mapper.Map <InsertCustomerVipModel, InsertCustomerVipRequest>(model));

                if (result.IsSuccess)
                {
                    TempData["Update"] = result.ToJson();
                    return(RedirectToAction("Index",
                                            "CustomerVip"));
                }

                ModelState.AddModelError("",
                                         result.Message);
            }

            return(View(model));
        }
Beispiel #3
0
 public async Task <ActionResult <bool> > Add(InsertCustomerVipModel request)
 {
     return(Ok(await _customerVipService.Add(await GetUserId(),
                                             request,
                                             GetPermissionId())));
 }
        public async Task <TResponse <bool> > Add(int userId,
                                                  InsertCustomerVipModel request,
                                                  int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

                if (checkValid.IsSuccess)
                {
                    var canInsert = await CanInsert(request);

                    if (canInsert.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteScalarAsync <int>(SqlQuery.CUSTOMER_VIP_INSERT,
                                                                                    new
                        {
                            request.Name,
                            request.Color,
                            request.SortOrder,
                            request.Profit,
                            UserCreated = userId,
                            DateCreated = DateTime.Now,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                #region Update redis cache

                                await _customerVipCacheService.AddOrUpdate(new CustomerVipCacheModel
                                {
                                    Id        = result.Data,
                                    Name      = request.Name,
                                    Color     = request.Color,
                                    SortOrder = request.SortOrder,
                                    Profit    = request.Profit
                                });

                                #endregion

                                return(await Ok(true));
                            }

                            return(await Fail <bool>(ErrorEnum.BAD_REQUEST.GetStringValue()));
                        }

                        return(await Fail <bool>(result.Message));
                    }

                    return(await Fail <bool>(canInsert.Message));
                }

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }