Esempio n. 1
0
        public async Task <IActionResult> CreateCampaign(CreateNewCampaignRequest newCampaignRequest)
        {
            var result = await _dbService.CreateCampaignAsync(newCampaignRequest);

            ObjectResult response = new ObjectResult(result)
            {
                StatusCode = 201
            };

            return(response);
        }
Esempio n. 2
0
 public IActionResult CreateNewCampaings(CreateNewCampaignRequest request)
 {
     try
     {
         var result = _context.CreateNewCampaign(request);
         return(Ok(result));
     }
     catch (ClientNotExistsException exc)
     {
         return(BadRequest(exc.Message));
     }
     catch (BuildingAreNotOnTheSameStreet exc)
     {
         return(BadRequest(exc.Message));
     }
 }
Esempio n. 3
0
 public IActionResult CreateCampaign(CreateNewCampaignRequest request)
 {
     try
     {
         var response = _dbService.CreateCampaign(request);
         return(CreatedAtAction("CreateCampaign", response));
     }
     catch (BuildingDoesntExistException e)
     {
         return(BadRequest(e.Message));
     }
     catch (BuildingsOnDifferentStreetsException e)
     {
         return(BadRequest(e.Message));
     }
     catch (UserDoesntExistExcetion e)
     {
         return(BadRequest(e.Message));
     }
 }
Esempio n. 4
0
        public Campaign CreateNewCampaign(CreateNewCampaignRequest request)
        {
            var clientInfo = _context.Clients.Where(m => m.IdClient == request.IdClient).FirstOrDefault();

            if (clientInfo == null)
            {
                throw new ClientNotExistsException($"Client with id={request.IdClient} not exists");
            }

            var fromBuildingStreet = _context.Buildings.Where(m => m.IdBuilding == request.FromIdBuilding).FirstOrDefault();
            var toBuildingStreet   = _context.Buildings.Where(m => m.IdBuilding == request.ToIdBuilding).FirstOrDefault();

            if (!((toBuildingStreet.Street).Equals(fromBuildingStreet.Street)))
            {
                throw new BuildingAreNotOnTheSameStreet($"Buildings aren't on the same street, addresses you gave: {request.FromIdBuilding} and {request.ToIdBuilding}");
            }


            var campaing = new Campaign {
                IdClient            = request.IdClient,
                StartDate           = request.StartDate,
                EndDate             = request.EndDate,
                PricePerSquareMeter = request.PricePerSquareMeter,
                FromIdBuilding      = request.FromIdBuilding,
                ToIdBuilding        = request.ToIdBuilding
            };



            _context.Attach(campaing);
            _context.Add(campaing);
            _context.SaveChanges();


            return(campaing);
        }
Esempio n. 5
0
        public CreateNewCampaignResponse CreateCampaign(CreateNewCampaignRequest request)
        {
            var fromBuilding = _context.Building.FirstOrDefault(b => b.IdBuilding == request.FromIdBuilding);
            var toBuilding   = _context.Building.FirstOrDefault(b => b.IdBuilding == request.ToIdBuilding);
            var client       = _context.Client.SingleOrDefault(p => p.IdClient == request.IdClient);

            if (client == null)
            {
                throw new UserDoesntExistExcetion($"User with id = {request.IdClient} doesn't exist");
            }
            if (fromBuilding == null)
            {
                throw new BuildingDoesntExistException($"Building with id {request.FromIdBuilding} doesn't exist!");
            }
            if (toBuilding == null)
            {
                throw new BuildingDoesntExistException($"Building with id {request.ToIdBuilding} doesn't exist!");
            }
            if (request.StartDate > request.EndDate)
            {
                throw new WrongDateException($"Start date={request.StartDate} can't be grater than {request.EndDate}");
            }
            if (!fromBuilding.Street.Equals(toBuilding.Street) || !fromBuilding.City.Equals(toBuilding.City))
            {
                throw new BuildingsOnDifferentStreetsException("Buldings are on different streets!");
            }

            var newCampaign = new Campaign()
            {
                IdClient            = request.IdClient,
                StartDate           = request.StartDate,
                EndDate             = request.EndDate,
                PricePerSquareMeter = request.PricePerSquareMeter,
                FromIdBuilding      = request.FromIdBuilding,
                ToIdBuilding        = request.ToIdBuilding
            };

            var bannerBuildings = _context.Building
                                  .Where(b => b.StreetNumber >= fromBuilding.StreetNumber && b.StreetNumber <= toBuilding.StreetNumber)
                                  .OrderByDescending(b => b.Height)
                                  .ToList();

            var banerAreas = _calculatorService.CalculateMinBanerArea(bannerBuildings, fromBuilding, toBuilding);
            var newBanner1 = new Banner()
            {
                Name     = 1,
                Price    = banerAreas[0] * request.PricePerSquareMeter,
                Campaign = newCampaign,
                Area     = banerAreas[0]
            };

            var newBanner2 = new Banner()
            {
                Name     = 2,
                Price    = banerAreas[1] * request.PricePerSquareMeter,
                Campaign = newCampaign,
                Area     = banerAreas[1]
            };

            _context.Campaign.Add(newCampaign);
            _context.Banner.AddRange(newBanner1, newBanner2);
            _context.SaveChanges();

            return(new CreateNewCampaignResponse()
            {
                IdClient = request.IdClient,
                StartDate = request.StartDate,
                EndDate = request.EndDate,
                PricePerSquareMeter = request.PricePerSquareMeter,
                FromIdBuilding = request.FromIdBuilding,
                ToIdBuilding = request.ToIdBuilding,
                Banners = new List <Banner>()
                {
                    newBanner1, newBanner2
                },
                Price = (newBanner1.Area + newBanner2.Area) * request.PricePerSquareMeter
            });
        }
Esempio n. 6
0
        //==========Zadanie 8
        public async Task <NewlyCreatedCampaignResponse> CreateCampaignAsync(CreateNewCampaignRequest request)
        {
            if (await _dbcontext.Buildings.CountAsync() < 2)
            {
                throw new NotEnougBuildingsInTheDatabaseException("Not enough buildings in the database");
            }

            var isClientExists = await _dbcontext.Clients.AnyAsync(c => c.IdClient == request.IdClient);

            if (!isClientExists)
            {
                throw new ClientDoesNotExistsException($"Client with id={request.IdClient} does not exists");
            }

            var isBuildingFromBuildingExists = await _dbcontext.Buildings.AnyAsync(b => b.IdBuilding == request.FromIdBuilding);

            if (!isBuildingFromBuildingExists)
            {
                throw new BuildingDoesNotExistsException($"Building with an id={request.FromIdBuilding} does not exists");
            }

            var isBuildingToBuildingExists = await _dbcontext.Buildings.AnyAsync(b => b.IdBuilding == request.ToIdBuilding);

            if (!isBuildingFromBuildingExists)
            {
                throw new BuildingDoesNotExistsException($"Building with an id={request.ToIdBuilding} does not exists");
            }

            if (request.StartDate > request.EndDate)
            {
                throw new WrongDateException($"Start date={request.StartDate} can not be less than End date={request.EndDate}");
            }

            var fromBuilding = await _dbcontext.Buildings
                               .Where(b => b.IdBuilding == request.FromIdBuilding)
                               .SingleOrDefaultAsync();

            var toBuilding = await _dbcontext.Buildings
                             .Where(b => b.IdBuilding == request.ToIdBuilding)
                             .SingleOrDefaultAsync();

            if (!fromBuilding.City.Equals(toBuilding.City))
            {
                throw new BuildingsInDifferentCitiesException($"Building FromBuilding with an id={request.FromIdBuilding} and building ToBuilding with an id={request.ToIdBuilding} are located on different cities");
            }

            if (!fromBuilding.Street.Equals(toBuilding.Street))
            {
                throw new BuildingsOnDifferentStreetsException($"Building FromBuilding with an id={request.FromIdBuilding} and building ToBuilding with an id={request.ToIdBuilding} are located on different streets");
            }

            var campaign = new Campaing
            {
                IdClient            = request.IdClient,
                StartDate           = request.StartDate,
                EndDate             = request.EndDate,
                PricePerSquareMeter = request.PricePerSquareMeter,
                FromIdBuilding      = request.FromIdBuilding,
                ToIdBuilding        = request.ToIdBuilding
            };

            await _dbcontext.AddAsync(campaign);

            await _dbcontext.SaveChangesAsync();

            decimal[] areas = CalculateArea(fromBuilding, toBuilding);

            var banner1 = new Banner
            {
                Name       = "Banner1",
                Price      = areas[0] * request.PricePerSquareMeter,
                IdCampaing = campaign.IdCampaing,
                Area       = areas[0]
            };

            var banner2 = new Banner
            {
                Name       = "Banner2",
                Price      = areas[1] * request.PricePerSquareMeter,
                IdCampaing = campaign.IdCampaing,
                Area       = areas[1]
            };

            await _dbcontext.AddRangeAsync(banner1, banner2);

            await _dbcontext.SaveChangesAsync();

            return(new NewlyCreatedCampaignResponse
            {
                IdCampaing = campaign.IdCampaing,
                IdClient = campaign.IdClient,
                StartDate = campaign.StartDate,
                EndDate = campaign.EndDate,
                PricePerSquareMeter = campaign.PricePerSquareMeter,
                FromIdBuilding = campaign.FromIdBuilding,
                ToIdBuilding = campaign.ToIdBuilding,

                Banner1 = banner1,
                Banner2 = banner2,

                TotalPrice = (banner1.Area + banner2.Area) * request.PricePerSquareMeter
            });
        }