コード例 #1
0
ファイル: ClientDbService.cs プロジェクト: s17804/advert_api
        public AddNewClientResponseDto AddNewClient(AddNewClientRequestDto addNewClientRequestDto)
        {
            if (_advertDbContext.Clients.Any(c => c.Login.Equals(addNewClientRequestDto.Login)))
            {
                throw new ObjectAlreadyInDatabaseException("Login already in database");
            }

            var client = _mapper.Map <Client>(addNewClientRequestDto);
            var salt   = _passwordService.GenerateSalt();

            client.Salt     = salt;
            client.Password = _passwordService.CreateSaltedPasswordHash(addNewClientRequestDto.Password, salt);

            _advertDbContext.Add((object)client);
            _advertDbContext.SaveChanges();

            return(_mapper.Map <AddNewClientResponseDto>(client));
        }
コード例 #2
0
        public NewCampaignResponseDto AddNewCampaign(NewCampaignRequestDto newCampaignRequestDto)
        {
            if (newCampaignRequestDto.FromIdBuilding.Equals(newCampaignRequestDto.ToIdBuilding))
            {
                throw new BadRequestException("Selected only one building");
            }

            var startDate = DateTime.Parse(newCampaignRequestDto.StartDate);
            var endDate   = DateTime.Parse(newCampaignRequestDto.EndDate);

            var compareDate = DateTime.Compare(startDate, endDate);

            if (compareDate == 1 || compareDate == 0)
            {
                throw new BadRequestException("Bad date");
            }

            var client = _advertDbContext.Clients.FirstOrDefault(c =>
                                                                 c.IdClient.Equals(newCampaignRequestDto.IdClient));

            if (client == null)
            {
                throw new ResourceNotFoundException("Client not found");
            }

            var buildingFrom = _advertDbContext.Buildings.FirstOrDefault(b =>
                                                                         b.IdBuilding.Equals(newCampaignRequestDto.FromIdBuilding));

            if (buildingFrom == null)
            {
                throw new ResourceNotFoundException("Building not found");
            }

            var buildingTo = _advertDbContext.Buildings.FirstOrDefault(b =>
                                                                       b.IdBuilding.Equals(newCampaignRequestDto.ToIdBuilding));

            if (buildingTo == null)
            {
                throw new ResourceNotFoundException("Building not found");
            }

            if (!buildingFrom.City.Equals(buildingTo.City))
            {
                throw new BadRequestException("Building not located in same city");
            }

            if (!buildingFrom.Street.Equals(buildingTo.Street))
            {
                throw new BadRequestException("Building not located on same street");
            }

            var campaign = new Campaign
            {
                Client              = client,
                BuildingFrom        = buildingFrom,
                BuildingTo          = buildingTo,
                StartDate           = startDate,
                EndDate             = endDate,
                PricePerSquareMeter = newCampaignRequestDto.PricePerSquareMeter,
                Banners             = _bannerAreaCalculatorService.CalculateBanners(buildingFrom, buildingTo)
            };

            foreach (var campaignBanner in campaign.Banners)
            {
                campaignBanner.Price = campaignBanner.Area * campaign.PricePerSquareMeter;
            }

            _advertDbContext.Add(campaign);
            _advertDbContext.SaveChanges();

            return(_mapper.Map <NewCampaignResponseDto>(campaign));
        }