Ejemplo n.º 1
0
        public async Task <MessageDetailsViewModel> CreateMessageAsync(string senderId, string recipientId, int adId, string content)
        {
            if (!await context.Ads.AnyAsync(x => x.Id == adId))
            {
                throw new ArgumentException(GlobalConstants.InvalidAdIdErrorMessage);
            }

            var message = new Message
            {
                SenderId    = senderId,
                RecipientId = recipientId,
                AdId        = adId,
                Content     = content
            };

            await context.Messages.AddAsync(message);

            await context.SaveChangesAsync();

            var messageFromDb = context.Messages.FirstOrDefault(x => x.Id == message.Id);

            var messageViewModel = mapper.Map <MessageDetailsViewModel>(messageFromDb);
            var sender           = await usersService.GetUserByIdAsync(senderId);

            messageViewModel.Sender = sender.UserName;

            return(messageViewModel);
        }
Ejemplo n.º 2
0
        private async Task RemoveSellMeUserFavoriteProductAsync(int adId, string currentUserId)
        {
            var sellMeUserFavoriteProduct = context.SellMeUserFavoriteProducts.First(x => x.AdId == adId && x.SellMeUserId == currentUserId);

            context.Remove(sellMeUserFavoriteProduct);
            await context.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task CreatePromotionOrderAsync(int adId, int promotionId)
        {
            if (!await context.Ads.AnyAsync(x => x.Id == adId))
            {
                throw new ArgumentException(GlobalConstants.InvalidAdIdErrorMessage);
            }

            if (!await context.Promotions.AnyAsync(x => x.Id == promotionId))
            {
                throw new ArgumentException(GlobalConstants.InvalidPromotionIdErrorMessage);
            }

            var adFromDb = await adsService.GetAdByIdAsync(adId);

            var promotionFromDb = await GetPromotionByIdAsync(promotionId);

            var promotionOrder = new PromotionOrder
            {
                AdId        = adId,
                PromotionId = promotionId,
                CreatedOn   = DateTime.UtcNow,
                ActiveTo    = DateTime.UtcNow.AddDays(promotionFromDb.ActiveDays),
                Price       = promotionFromDb.Price
            };

            adFromDb.Updates += promotionFromDb.Updates;

            await context.PromotionOrders.AddAsync(promotionOrder);

            await context.SaveChangesAsync();
        }
Ejemplo n.º 4
0
        public async Task CreateReview(string ownerId, string creatorId, string content, int rating)
        {
            if (ownerId.IsNullOrEmpty() || creatorId.IsNullOrEmpty() || content.IsNullOrEmpty())
            {
                throw new ArgumentException(CreateReviewArgumentsNotNullOrEmptyErrorMessage);
            }

            if (rating < 1 || rating > 5)
            {
                throw new ArgumentException(ArgumentOutOfRangeErrorMessage);
            }

            if (ownerId == creatorId)
            {
                throw new InvalidOperationException(OwnerOfTheAdCantLeaveReviewErrorMessage);
            }

            var review = new Review
            {
                OwnerId   = ownerId,
                CreatorId = creatorId,
                Comment   = content,
                Rating    = rating
            };

            await context.Reviews.AddAsync(review);

            await context.SaveChangesAsync();
        }
Ejemplo n.º 5
0
        public async Task SeedAsync(SellMeDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(SellMeDbContextSeeder));

            var seeders = new List <ISeeder>
            {
                new RolesSeeder(),
                new CategoriesSeeder(),
                new SubcategoriesSeeder(),
                new ConditionsSeeder(),
                new PromotionsSeeder()
            };

            foreach (var seeder in seeders)
            {
                await seeder.SeedAsync(dbContext, serviceProvider);

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> BlockUserByIdAsync(string userId)
        {
            if (!await this.context.SellMeUsers.AnyAsync(x => x.Id == userId))
            {
                throw new ArgumentException(GlobalConstants.InvalidUserIdErrorMessage);
            }

            var userFromDb = await context.SellMeUsers.FirstOrDefaultAsync(x => x.Id == userId);

            userFromDb.IsDeleted = true;
            userFromDb.DeletedOn = DateTime.UtcNow;

            context.SellMeUsers.Update(userFromDb);
            await context.SaveChangesAsync();

            await DeleteAdsByUserId(userId);

            return(true);
        }
Ejemplo n.º 7
0
        public async Task CreateUpdateAdAsync(int adId)
        {
            if (!await context.Ads.AnyAsync(x => x.Id == adId))
            {
                throw new ArgumentException(GlobalConstants.InvalidAdIdErrorMessage);
            }

            var updateAd = new UpdateAd
            {
                AdId = adId
            };
            await context.UpdateAds.AddAsync(updateAd);

            await context.SaveChangesAsync();
        }
Ejemplo n.º 8
0
        public async Task CreateAdAsync(CreateAdInputModel inputModel)
        {
            var imageUrls = inputModel.CreateAdDetailInputModel.Images
                            .Select(async x =>
                                    await cloudinaryService.UploadPictureAsync(x, x.FileName))
                            .Select(x => x.Result)
                            .ToList();

            var ad = mapper.Map <Ad>(inputModel);

            ad.ActiveTo = DateTime.UtcNow.AddDays(GlobalConstants.AdDuration);
            ad.Images   = imageUrls.Select(x => new Image {
                ImageUrl = x
            })
                          .ToList();
            ad.SellerId = usersService.GetCurrentUserId();

            await context.Ads.AddAsync(ad);

            await context.SaveChangesAsync();
        }