예제 #1
0
        public async Task <IActionResult> AddAdvertisement(AddAdvertisementRequest request)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!ModelState.IsValid)
            {
                return(View());
            }

            var userId = User.Claims.GetUserId();

            try
            {
                await _adService.AddAdvertisement(request, userId);
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                return(View());
            }

            return(RedirectToAction("Index"));
        }
예제 #2
0
        /// <summary>
        /// Добавляет объявление
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <int> Add(AddAdvertisementRequest request)
        {
            using (var transaction = await _dataContext.Database.BeginTransactionAsync(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    var currentCount = await _dataContext.Advertisements.CountAsync(a => a.UserId == _user.Id);

                    if (currentCount >= _options.MaxAdsPerUser)
                    {
                        throw new Exception("Вы исчерпали лимит объявлений.");
                    }

                    var id = Guid.NewGuid();
                    var saveFileRequest = new SaveFileRequest
                    {
                        FileName      = $"{id}",
                        DirectoryName = FileDirectories.Advertisements,
                        UploadedFile  = request.Image
                    };

                    var filesInfo = request.Image != null ? await _fileService.Save(saveFileRequest) : new SaveFileResponse
                    {
                        Url = "", Extension = ""
                    };

                    var advertisement = new Data.Entities.Advertisement
                    {
                        Id             = id,
                        ImageUrl       = filesInfo.Url,
                        ImageExtension = filesInfo.Extension,
                        ImagePath      = filesInfo.Path,
                        Text           = request.Text,
                        UserId         = _user.Id
                    };

                    await _dataContext.AddAsync(advertisement);

                    await _dataContext.SaveChangesAsync();

                    await transaction.CommitAsync();

                    return(advertisement.Number);
                }
                catch (Exception exception)
                {
                    await transaction.RollbackAsync();

                    _logger.LogError(exception.Message);
                    throw exception;
                }
            }
        }
        public async Task <ActionResult <int> > Add([FromForm] AddAdvertisementRequest request)
        {
            try
            {
                var res = await _advService.Add(request);

                return(new OkObjectResult(res));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                throw exception;
            }
        }
예제 #4
0
        public async Task AddAdvertisement(AddAdvertisementRequest request, Guid userId)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            using var transaction = _dataProvider.CreateTransaction(IsolationLevel.Serializable);

            var adsCount = _dataProvider.Get <AdDb>(i => i.UserId == userId).Count();

            if (adsCount >= _userOptions.UserAdsCountLimit)
            {
                throw new Exception($"You cannot add more than {_userOptions.UserAdsCountLimit} ads");
            }

            string imagePath = null;

            if (request.Image != null)
            {
                imagePath = _imageHelper.UploadImageAndGetName(request.Image).Result;
            }

            var currentAd = new AdDb
            {
                ImagePath   = imagePath,
                CreatedDate = DateTime.Now,
                Title       = request.Title,
                Text        = request.Text,
                UserId      = userId
            };

            await _dataProvider.Insert(currentAd);

            transaction.Commit();
        }