コード例 #1
0
        public TokenResponseDto LogIn(LogInRequestDto logInRequestDto)
        {
            var client = _advertDbContext.Clients.FirstOrDefault(c => c.Login.Equals(logInRequestDto.Login));

            if (client == null)
            {
                throw new BadLoginOrPasswordException("Bad Login or Password");
            }

            var salt           = client.Salt;
            var storedPassword = client.Password;

            if (!_passwordService.ValidatePassword(logInRequestDto.Password, storedPassword, salt))
            {
                throw new BadLoginOrPasswordException("Bad Login or Password");
            }

            var accessToken  = CreateJwtToken(logInRequestDto.Login);
            var refreshToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

            client.RefreshToken = refreshToken;

            _advertDbContext.SaveChanges();

            return(new TokenResponseDto
            {
                AccessToken = accessToken,
                RefreshToken = refreshToken
            });
        }
コード例 #2
0
        public Client AddClient(ClientRegistrationRequest request)
        {
            var _salt         = Password.CreateSalt();
            var _passwordHash = Password.CreatePasswordHash(request.Password, _salt);

            if (_context.Clients.FirstOrDefault(c => c.Login == request.Login) != null)
            {
                throw new ClientAlreadyExistException($"Client with login = {request.Login} already exists");
            }

            var _client = _context.Clients.Add(new Client {
                FirstName = request.FirstName,
                LastName  = request.LastName,
                Email     = request.Email,
                Phone     = request.Phone,
                Login     = request.Login,
                Password  = _passwordHash,
                Salt      = _salt
            }).Entity;

            _context.SaveChanges();

            return(_client);
        }
コード例 #3
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));
        }
コード例 #4
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));
        }