public async Task <bool> LogInAsync(LoginRequestDTO loginRequest, IOutputPort <LoginResponseDTO> outputPort) { try { _logger.LogInformation("Logging user for request {@request}", loginRequest); if (loginRequest.Username.IsValidString() && loginRequest.Password.IsValidString()) { var user = await _userRepository.FindByNameAsync(loginRequest.Username); if (user != null) { if (await _userRepository.CheckPasswordAsync(user, loginRequest.Password)) { var token = await _jwtFactory.GenerateTokenAsync(user.Id, user.UserName); _logger.LogInformation("Login succesfull, token generated succesful"); outputPort.CreateResponse(new LoginResponseDTO(token, true)); return(true); } } } } catch (Exception ex) { _logger.LogInformation(ex.Message); outputPort.CreateResponse(new LoginResponseDTO(false, new ErrorResponse(new[] { GlobalErrors.UnexpectedError }))); return(false); } outputPort.CreateResponse(new LoginResponseDTO(false, new ErrorResponse(new[] { new Error(GlobalErrorCodes.InvalidCredentials, "Username or password is invalid") }))); return(false); }
public async Task <bool> RegisterAsync(RegisterRequestDTO loginRequest, IOutputPort <RegisterResponseDTO> outputPort) { try { _logger.LogInformation("Creating user for request {@request}", loginRequest); var result = await _userRepository.CreateUserAsync(new User(loginRequest.FirstName, loginRequest.LastName, loginRequest.Email, loginRequest.Username), loginRequest.Password); _logger.LogInformation("User repository returned {@response}", result); if (result) { outputPort.CreateResponse(new RegisterResponseDTO(true)); } else { outputPort.CreateResponse(new RegisterResponseDTO(false, new ErrorResponse(new List <Error> { new Error(GlobalErrorCodes.InternalServer, "Cannot create new user") }))); } return(result); } catch (Exception ex) { _logger.LogError(ex.Message); outputPort.CreateResponse(new RegisterResponseDTO(false, new ErrorResponse(new List <Error> { GlobalErrors.UnexpectedError }))); return(false); } }
public async Task <bool> GetUserAsync(string name, IOutputPort <GetUserResponseDTO> outputPort) { try { if (name.IsValidString()) { var user = await _userRepository.FindByNameAsync(name); if (user != null) { outputPort.CreateResponse(new GetUserResponseDTO(user.WithoutPassword(), true)); return(true); } } } catch (Exception ex) { _logger.LogError(ex.Message); outputPort.CreateResponse(new GetUserResponseDTO(false, new ErrorResponse(new[] { GlobalErrors.UnexpectedError }))); return(false); } outputPort.CreateResponse(new GetUserResponseDTO(false, new ErrorResponse(new[] { new Error(GlobalErrorCodes.NotFound, $"User with userName {name} does not exist") }))); return(false); }
public async Task GetCurrentSpotsEntriesAsync(int parkingLotId, string spotId, IOutputPort <GetParkingDataResponseDTO> outputPort) { try { _logger.LogInformation($"Getting current spot entries for parking lot {parkingLotId} and spotName {spotId}"); var spots = new List <ParkingSpot>(); if (spotId == null) { spots.AddRange(await _spotsRepo.GetParkingSpotsWithLastEntriesAsync(parkingLotId)); } else { spots.Add(await _spotsRepo.GetParkingSpotWithLastEntryAsync(parkingLotId, spotId)); } _logger.LogInformation($"Creating parking data response"); outputPort.CreateResponse(new GetParkingDataResponseDTO(spots, true)); } catch (NotFoundException) { var meesage = $"ParkingLot with id {parkingLotId} does not exist"; _logger.LogInformation(meesage); outputPort.CreateResponse(new GetParkingDataResponseDTO(false, new ErrorResponse(new[] { new Error(GlobalErrorCodes.NotFound, meesage) }))); } catch (Exception ex) { _logger.LogError(ex, "Error while getting parking entries"); outputPort.CreateResponse(new GetParkingDataResponseDTO(false, new ErrorResponse(new[] { GlobalErrors.UnexpectedError }))); } }
public async Task GetParkingLotByIdAsync(int?parkingLotId, IOutputPort <GetParkingLotsResponseDTO> outputPort) { try { _logger.LogInformation($"Getting parking lot with id {parkingLotId}"); var parkingLots = new List <ParkingLot>(); if (parkingLotId != null) { parkingLots.Add(await _lotRepo.GetByIdAsync(parkingLotId)); } else { parkingLots.AddRange(await _lotRepo.GetAllParkingLotsAsync()); } outputPort.CreateResponse(new GetParkingLotsResponseDTO(parkingLots, true)); _logger.LogInformation($"Getting parking lot ended sucessfully"); } catch (NotFoundException) { _logger.LogInformation($"ParkingLot with id {parkingLotId} does not exist"); outputPort.CreateResponse(new GetParkingLotsResponseDTO(false, new ErrorResponse(new[] { new Error(GlobalErrorCodes.NotFound, $"ParkingLot with id {parkingLotId} does not exist") }))); } catch (Exception ex) { _logger.LogError(ex, "Error while getting parking lot"); outputPort.CreateResponse(new GetParkingLotsResponseDTO(false, new ErrorResponse(new[] { GlobalErrors.UnexpectedError }))); } }
public async Task GetSpotsEntriesAsync(int parkingLotId, DateTime?from, DateTime?to, IEnumerable <string> spotName, IOutputPort <GetParkingDataResponseDTO> outputPort) { try { var res = await _spotsRepo.GetParkingSpotWithEntriesAsync(parkingLotId, from, to, spotName); outputPort.CreateResponse(new GetParkingDataResponseDTO(res, true)); } catch (Exception ex) { _logger.LogError(ex, "Error while getting parking entries"); outputPort.CreateResponse(new GetParkingDataResponseDTO(false, new ErrorResponse(new[] { GlobalErrors.UnexpectedError }))); } }
public async Task AddParkingLotAsync(AddParkingLotRequestDTO request, IOutputPort <StandardResponse> outputPort) { try { _logger.LogInformation("Inserting new ParkingLot. {@ParkingLot}", request.ParkingLot); await _lotRepo.InsertAsync(request.ParkingLot); outputPort.CreateResponse(new StandardResponse(true, "ParkingLot created successfully")); _logger.LogInformation($"Inserting new parking lot ended sucessfully"); } catch (Exception ex) { _logger.LogError(ex, "Error while inserting new parking lot"); outputPort.CreateResponse(new StandardResponse(false, new ErrorResponse(new[] { GlobalErrors.UnexpectedError }))); } }