public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateReceiptDescriptionModel request,
                                                     int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

                if (checkValid.IsSuccess)
                {
                    var canUpdate = await CanUpdate(request);

                    if (canUpdate.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.RECEIPT_DESCRIPTION_UPDATE,
                                                                        new
                        {
                            request.Id,
                            request.Name,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

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

                                await _receiptDescriptionCacheService.AddOrUpdate(new ReceiptDescriptionCacheModel
                                {
                                    Id   = request.Id,
                                    Name = request.Name
                                });

                                #endregion

                                return(await Ok(true));
                            }

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

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

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

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
        private async Task <TResponse <bool> > CanUpdate(UpdateReceiptDescriptionModel request)
        {
            try
            {
                var receiptDescription = await _receiptDescriptionCacheService.GetByName(request.Name);

                if (receiptDescription != null &&
                    receiptDescription.Id != request.Id)
                {
                    return(await Fail <bool>(ErrorEnum.RECEIPT_DESCRIPTION_HAS_EXIST.GetStringValue()));
                }

                return(await Ok(true));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
예제 #3
0
 public async Task <ActionResult <bool> > Update(UpdateReceiptDescriptionModel request)
 {
     return(Ok(await _receiptDescriptionService.Update(await GetUserId(),
                                                       request,
                                                       GetPermissionId())));
 }