Ejemplo n.º 1
0
        public override void Validate(string userName, string password)
        {
            try
            {
                logger.LogNewMessage($"Validating username and password for user {userName}", LogType.INFO);

                using (var dbContext = factory.GetContext())
                {
                    var user = dbContext.Users.Where(x => x.UserName == userName).SingleOrDefault();

                    if (user == null || user == new User())
                    {
                        var errorMessage = $"User with username {userName} does not exist in database. Access denied.";
                        logger.LogNewMessage(errorMessage, LogType.WARNING);
                        throw new FaultException(errorMessage);
                    }

                    if (!user.CheckPassword(password))
                    {
                        var errorMessage = $"Wrong password for user  {userName}. Access denied.";
                        logger.LogNewMessage(errorMessage, LogType.WARNING);
                        throw new FaultException(errorMessage);
                    }
                }

                logger.LogNewMessage($"User {userName} authenticated.", LogType.INFO);
            }catch (Exception ex)
            {
                logger.LogNewMessage($"Authentication exception. Error {ex.Message}", LogType.ERROR);
                throw new FaultException(ex.Message);
            }
        }
 public void CloseServiceChannel()
 {
     try
     {
         logger.LogNewMessage($"Closing service host for host for type {contract}", LogType.INFO);
         serviceHost.Close();
         logger.LogNewMessage($"Successfully closed host for type {contract}", LogType.INFO);
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Failed to close service host for type {contract}. ERROR message {ex.Message}", LogType.ERROR);
     }
 }
 ///
 /// <param name="id"></param>
 public Country GeCountryByID(int id)
 {
     try
     {
         logger.LogNewMessage($"Getting country by id {id}.", LogType.INFO);
         using (var dbContext = factory.GetContext())
         {
             return(dbContext.Countries.Find(id));
         }
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Error occured getting country by id {id}. Message {ex.Message}", LogType.ERROR);
         throw new FaultException(ex.Message);
     }
 }
Ejemplo n.º 4
0
        private void InitializeServices()
        {
            try
            {
                logger.LogNewMessage($"Initializing service providers.", LogType.INFO);

                countryService = new CountryServiceProvider(ServerLogger.GetOrCreate(), GetDatabaseContextFactory());
                placeService   = new PlaceServiceProvider(ServerLogger.GetOrCreate(), GetDatabaseContextFactory());
                roadService    = new RoadServiceProvider(ServerLogger.GetOrCreate(), GetDatabaseContextFactory());
                stationService = new StationServiceProvider(ServerLogger.GetOrCreate(), GetDatabaseContextFactory());
                trackService   = new TrackServiceProvider(ServerLogger.GetOrCreate(), GetDatabaseContextFactory());
                userService    = new UserServiceProvider(ServerLogger.GetOrCreate(), GetDatabaseContextFactory());
            }catch (Exception ex)
            {
                logger.LogNewMessage($"Error occured initializing service providers. ERROR {ex.Message}", LogType.ERROR);
            }
        }
 ///
 /// <param name="station"></param>
 public bool AddStation(Station station)
 {
     try
     {
         logger.LogNewMessage($"Adding new station to database", LogType.INFO);
         using (var dbContext = factory.GetContext())
         {
             foreach (Track track in station.Tracks)
             {
                 dbContext.Track.Attach(track);
                 track.StationId = station.Id;
             }
             dbContext.Stations.Attach(station);
             dbContext.Places.Attach(station.Place);
             dbContext.Stations.Add(station);
             dbContext.SaveChanges();
         }
         logger.LogNewMessage($"Station added successfully.", LogType.INFO);
         return(true);
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Error occured, station couldn't be added. Message {ex.Message}", LogType.ERROR);
         throw new FaultException(ex.Message);
     }
 }
 ///
 /// <param name="user"></param>
 public bool AddUser(User user)
 {
     try
     {
         logger.LogNewMessage($"Adding new user with username {user.UserName} to the database", LogType.INFO);
         using (var dbContext = factory.GetContext())
         {
             dbContext.Users.Add(user);
             dbContext.SaveChanges();
         }
         logger.LogNewMessage($"User added successfully.", LogType.INFO);
         return(true);
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Error occured, user couldn't be added. Message {ex.Message}", LogType.ERROR);
         throw new FaultException(ex.Message);
     }
 }
 ///
 /// <param name="place"></param>
 public bool AddPlace(Place place)
 {
     try
     {
         logger.LogNewMessage($"Adding new with name {place.Name} and id {place.Id} to database..", LogType.INFO);
         using (var dbContext = factory.GetContext())
         {
             dbContext.Places.Attach(place);
             dbContext.Places.Add(place);
             dbContext.SaveChanges();
         }
         logger.LogNewMessage($"Place added.", LogType.INFO);
         return(true);
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Error occured, place couldn't be added. ERROR message : {ex.Message}", LogType.ERROR);
         throw new FaultException(ex.Message);
     }
 }
        private void InitializeCountries()
        {
            //Initialize countries, countries were retrieved from https://textlists.info/geography/countries-of-the-world/
            logger.LogNewMessage($"Initializing countries..", LogType.INFO);


            try
            {
                var fileContent    = File.ReadAllLines("countries.txt");
                var worldCountries = new List <string>(fileContent);
                logger.LogNewMessage($"Fetching countries from repository", LogType.DEBUG);
                using (var dbContext = factory.GetContext())
                {
                    var countries = dbContext.Countries.ToList();

                    List <Country> countriesToAdd = new List <Country>();
                    foreach (string countryName in worldCountries)
                    {
                        if (countries.Where(x => x.Name == countryName).Count() == 0)
                        {
                            countriesToAdd.Add(new Country(0, countryName));
                        }
                    }

                    if (countriesToAdd.Count > 0)
                    {
                        dbContext.Countries.AddRange(countriesToAdd);
                    }
                    dbContext.SaveChanges();

                    logger.LogNewMessage($"Countries intialized successfully.", LogType.INFO);
                }
            }
            catch (Exception ex)
            {
                logger.LogNewMessage($"Error occured trying to intialize countries. Message {ex.Message}", LogType.ERROR);
            }
        }
Ejemplo n.º 9
0
 ///
 /// <param name="road"></param>
 public Road AddRoad(Road road)
 {
     try
     {
         logger.LogNewMessage($"Adding new road to database", LogType.INFO);
         using (var dbContext = factory.GetContext())
         {
             foreach (Station s in road.Stations)
             {
                 dbContext.Stations.Attach(s);
             }
             dbContext.Roads.Add(road);
             dbContext.SaveChanges();
             logger.LogNewMessage($"Road added.", LogType.INFO);
             return(road);
         }
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Error occured, couldn't add entity. Error message {ex.Message}", LogType.ERROR);
         throw new FaultException(ex.Message);
     }
 }
 ///
 /// <param name="track"></param>
 public bool AddTrack(Track track)
 {
     try
     {
         logger.LogNewMessage($"Adding new track to the database", LogType.INFO);
         using (var dbContext = factory.GetContext())
         {
             dbContext.Track.Attach(track);
             dbContext.Track.Add(track);
             dbContext.SaveChanges();
         }
         logger.LogNewMessage($"Track added successfully.", LogType.INFO);
         return(true);
     }
     catch (Exception ex)
     {
         logger.LogNewMessage($"Error occured, track couldn't be added. Message {ex.Message}", LogType.ERROR);
         throw new FaultException(ex.Message);
     }
 }