public async Task <IActionResult> Add([FromBody] ApartmentToAddDto dto)
        {
            var result = await repo.Add(dto);

            if (result)
            {
                return(Ok());
            }
            return(BadRequest());
        }
Example #2
0
        public async Task <bool> Add(ApartmentToAddDto dto)
        {
            var result = await Exist(dto.Name);

            if (!result)
            {
                Apartment apm = new Apartment()
                {
                    City      = dto.City,
                    Country   = dto.Country,
                    Guests    = dto.Guests,
                    IsDeleted = false,
                    Lat       = dto.Lat,
                    Lon       = dto.Lon,
                    Name      = dto.Name,
                    Number    = dto.Number,
                    PostCode  = dto.PostCode,
                    Price     = dto.Price,
                    Rooms     = dto.Rooms,
                    Status    = ApartmentStatus.InActive,
                    Street    = dto.Street,
                    TimeIn    = dto.TimeIn,
                    TimeOut   = dto.TimeOut,
                    Type      = dto.Type
                };

                apm.Host = await context.Hosts.SingleAsync(x => x.Username == dto.HostUsername);

                apm.HostId    = apm.Host.Id;
                apm.Comments  = new Collection <Comment>();
                apm.Amenities = new List <Amenitie>();
                foreach (var item in dto.Amenities)
                {
                    var res = context.Amenities.FirstOrDefault(x => x.Name == item.Name);
                    if (res != null)
                    {
                        apm.Amenities.Add(new Amenitie()
                        {
                            Name = res.Name
                        });
                    }
                }

                apm.Photos = dto.Photos.ToList();
                apm.Dates  = ParseDates(dto.Dates);

                context.Apartments.Add(apm);
                await context.SaveChangesAsync();

                return(true);
            }
            return(false);
        }