Exemple #1
0
        public void Post([FromBody] WeatherVM weather)
        {
            if (ModelState.IsValid)
            {
                if (weather.key >= 0 && weather.weatherText != String.Empty && weather.temperature != null)
                {
                    // Make sure current location is not exists in DB.
                    Locations locationFromDB = db.Locations.FirstOrDefault(i => i.LocationId == weather.key);

                    // Important !!!
                    // We are not supposed to get here.
                    // Check if we found location in DB --> Do nothing
                    if (locationFromDB != null)
                    {
                        return;
                    }


                    // Add location to favorites
                    db.Locations.Add(new Locations()
                    {
                        LocationId  = weather.key,
                        Name        = weather.name,
                        Temperature = (int)weather.temperature,
                        WeatherText = weather.weatherText
                    });


                    db.SaveChanges();
                }
            }
        }
Exemple #2
0
        [ActionName("register")] //api/users/register
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            try
            {
                var dbContext = new WeatherAppContext();
                using (dbContext)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateName(model.Name);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();
                    var user            = dbContext.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("Username is already taken!");
                    }

                    user = new User()
                    {
                        Username = usernameToLower,
                        Name     = model.Name,
                        AuthCode = model.AuthCode
                    };

                    dbContext.Users.Add(user);
                    dbContext.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    dbContext.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        Name       = user.Name,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                                                               loggedModel);
                    return(response);
                }
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest,
                                                           ex.Message);
                return(response);
            }
        }
Exemple #3
0
        [ActionName("logout")]  //api/users/logout/{sessionKey}
        public HttpResponseMessage PutLogoutUser(string sessionKey)
        {
            try
            {
                var context = new WeatherAppContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentException("Invalid user authentication.");
                    }

                    user.SessionKey = null;
                    context.SaveChanges();

                    var response = this.Request.CreateResponse(HttpStatusCode.OK);
                    return(response);
                }
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return(response);
            }
        }
        public async Task <IActionResult> ReceiveNewUserAndPutInDatabase([FromBody] User user)
        {
            if (user == null)
            {
                return(await Task.Run(() => BadRequest()));
            }

            context.Users.Add(user);
            context.SaveChanges();

            return(Ok());
        }
        public async Task <IActionResult> ReceiveNewPostAndPutInDatabase([FromBody] Post post)
        {
            if (post == null)
            {
                return(await Task.Run(() => BadRequest()));
            }

            context.Posts.Add(post);
            context.SaveChanges();

            return(Ok());
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            using (var db = new WeatherAppContext())
            {
                // Create and save a new Blog
                Console.Write("Enter Stop to Stop: ");
                var stop = Console.ReadLine();

                while (!stop.Equals("Stop"))
                {
                    Console.WriteLine("Enter Firstname: ");
                    var fn = Console.ReadLine();
                    Console.WriteLine("Enter Lastname: ");
                    var ln = Console.ReadLine();
                    Console.WriteLine("Enter Email: ");
                    var em = Console.ReadLine();
                    Console.WriteLine("Enter Username: "******"Enter Password: "******"Enter Stop to Stop: ");
                    stop = Console.ReadLine();
                }

                // // Display all Blogs from the database
                // var query = from b in db.Users
                //             select b;

                // Console.WriteLine("All users in the database:");
                // foreach (var item in query)
                // {
                //     Console.WriteLine(item.FirstName);
                // }

                // Console.WriteLine("Press any key to exit...");
                // Console.ReadKey();
            }
        }
Exemple #7
0
        [ActionName("login")]  //api/users/login
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            try
            {
                ValidateUsername(model.Username);
                ValidateAuthCode(model.AuthCode);

                var context = new WeatherAppContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.Username == model.Username.ToLower() &&
                                                            u.AuthCode == model.AuthCode);

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }
                    if (user.SessionKey == null)
                    {
                        user.SessionKey = this.GenerateSessionKey(user.Id);
                        context.SaveChanges();
                    }

                    var loggedModel = new LoggedUserModel()
                    {
                        Name       = user.Name,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                                                               loggedModel);
                    return(response);
                }
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest,
                                                           ex.Message);
                return(response);
            }
        }