Esempio n. 1
0
        public async Task <ActionResult <CreateApartamentOutputModel> > Create(ApartamentInputModel input)
        {
            var dealer = await this.dealers.FindByUser(this.currentUser.UserId);

            var category = await this.categories.Find(input.Category);

            if (category == null)
            {
                return(BadRequest(Result.Failure("Category does not exist.")));
            }

            var apartament = new Apartament
            {
                Dealer      = dealer,
                Information = input.Information,
                Category    = category,
                ImageUrl    = input.ImageUrl,
                PricePerDay = input.PricePerDay,
                Options     = new Options
                {
                    HasClimateControl = input.HasClimateControl,
                    NumberOfBeds      = input.NumberOfBeds,
                    Floor             = input.Floor
                }
            };

            var messageData = new ApartamentCreatedMessage()
            {
                ApartamentId = apartament.Id,
                Information  = apartament.Information
            };

            var message = new Message(messageData);

            await this.apartaments.Save(apartament, message);

            await this.massTransitBus.Publish(messageData);

            await this.apartaments.MarkMessageAsPublished(message.Id);

            return(new CreateApartamentOutputModel(apartament.Id));
        }
Esempio n. 2
0
        public async Task <ActionResult> Edit(int id, ApartamentInputModel input)
        {
            var dealerId = await this.dealers.GetIdByUser(this.currentUser.UserId);

            var dealerHasApartament = await this.dealers.HasApartament(dealerId, id);

            if (!dealerHasApartament)
            {
                return(BadRequest(Result.Failure("You cannot edit this apartament.")));
            }

            var category = await this.categories.Find(input.Category);

            var apartament = await this.apartaments.Find(id);

            apartament.Information = input.Information;
            apartament.Category    = category;
            apartament.ImageUrl    = input.ImageUrl;
            apartament.PricePerDay = input.PricePerDay;
            apartament.Options     = new Options
            {
                HasClimateControl = input.HasClimateControl,
                NumberOfBeds      = input.NumberOfBeds,
                Floor             = input.Floor
            };

            await this.apartaments.Save(apartament);

            await this.massTransitBus.Publish(new ApartamentUpdatedMessage()
            {
                ApartamentId = apartament.Id,
                Information  = apartament.Information
            });

            return(Result.Success);
        }