示例#1
0
        public async Task <bool> Create(AdCreateIm input)
        {
            var category = await this.db.Categories.FirstOrDefaultAsync(c => c.Id == input.CategoryId);

            if (category == null)
            {
                throw new InvalidOperationException($"Category with id {input.CategoryId} doesn't exits");
            }

            var ad = new Ad
            {
                Title       = input.Title,
                Price       = input.Price,
                Description = input.Description,
                ImageUrl    = input.ImageUrl,
                IsActive    = input.IsActive,
                CategoryId  = input.CategoryId,
                UserId      = user.Id
            };


            category.Ads.Add(ad);
            this.db.Ads.Add(ad);

            await this.db.SaveChangesAsync();

            var message = new AdCreatedMessage
            {
                Id       = ad.Id,
                Title    = ad.Title,
                Price    = ad.Price,
                Category = category.Name
            };

            await this.publisher.Publish(message);

            var dbMessage = new Message(message);

            dbMessage.MarkAsPublished();
            this.db.Messages.Add(dbMessage);

            await this.db.SaveChangesAsync();

            return(true);
        }
示例#2
0
 public async Task <ActionResult <bool> > Create(AdCreateIm input)
 => await this.ads.Create(input);