public IHttpActionResult register()
        {
            string email    = formData("email");
            string password = formData("password");

            int existingUsers = db.users.Where(userFind => userFind.email == email).Count();

            if (existingUsers > 0)
            {
                return(Content(HttpStatusCode.Conflict, "User alredy exist"));
            }

            //Creat new user
            users user = new users();

            user.email = email;

            db.users.Add(user);
            db.SaveChanges();

            // TODO: Add password as authentication

            Dictionary <string, string> respons = new Dictionary <string, string>();

            respons.Add("id", user.id.ToString());
            respons.Add("token", user.email);
            respons.Add("email", user.email);

            return(Ok(respons));
        }
        public IHttpActionResult GetImport(string movieTag)
        {
            string apikey  = ConfigurationManager.AppSettings["apikey"];
            string baseUrl = "https://api.themoviedb.org/3/find/%IMDBID%?external_source=imdb_id&api_key=%APIKEY%";
            string url     = baseUrl.Replace("%IMDBID%", movieTag).Replace("%APIKEY%", apikey);
            string json    = "";

            using (WebClient fetcher = new WebClient())
            {
                json = fetcher.DownloadString(url);
            }

            if (json == "")
            {
                return(Content(HttpStatusCode.ServiceUnavailable, "Api not responding"));
            }

            dynamic data = JsonConvert.DeserializeObject(json);

            if (data.movie_results == null || data.movie_results.Count < 1)
            {
                return(Content(HttpStatusCode.NotFound, "Movie tag not fund"));
            }
            dynamic movieResult = data.movie_results[0];

            //if movie is alredy in data base do not make a duplicate
            IQueryable <movies> querry = db.movies.Where(movieFind => movieFind.imdb_tag == movieTag);
            int    existingMovie       = querry.Count();
            movies movie = null;

            if (existingMovie < 1)
            {
                //Add new movie
                movie          = new movies();
                movie.imdb_tag = movieTag;
                db.movies.Add(movie);
            }
            else
            {
                movie = querry.First <movies>();
            }

            movie.title       = movieResult.title;
            movie.description = movieResult.overview;
            movie.language    = movieResult.original_language;

            movie.premiere = DateTime.ParseExact("" + movieResult.release_date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

            movie.image_url = "https://image.tmdb.org/t/p/w185_and_h278_bestv2/" + movieResult.poster_path;
            db.SaveChanges();
            return(Ok(movie));
        }
Пример #3
0
        public IHttpActionResult booking()
        {
            string token   = formData("token");
            string email   = formData("email");
            long   show_id = long.Parse(formData("show_id"));
            int    seats   = int.Parse(formData("seats"));

            users user;

            if (token == null)
            {
                user = UserController.userByEmail(db, email);

                if (user == null)
                {
                    //Creat new user
                    user       = new users();
                    user.email = email;

                    db.users.Add(user);
                    db.SaveChanges();
                }
            }
            else
            {
                user = UserController.userByToken(db, token);

                if (user == null)
                {
                    return(Content(HttpStatusCode.Forbidden, "You took a wrong turn somewere"));
                }
            }

            shows show = db.shows.Find(show_id);

            if (show == null)
            {
                return(Content(HttpStatusCode.BadRequest, "Show not fund"));
            }

            if (DateTime.Compare(show.start_at, DateTime.Now) < 0)
            {
                return(Content(HttpStatusCode.Gone, "Show alredy started"));
            }

            auditoriums auditorium = db.auditoriums.Find(show.auditorium_id);

            IQueryable <reservations> querry = db.reservations.Where(reservations => reservations.show_id == show_id);
            int seatsTaken     = querry.Sum(reservations => reservations.seat_count);
            int seatsRemaining = auditorium.seats_total - seatsTaken;

            if (seats > seatsRemaining)
            {
                return(Content(HttpStatusCode.Conflict, "Not enough seats available"));
            }

            reservations reservation = new reservations();

            reservation.user_id           = user.id;
            reservation.show_id           = show_id;
            reservation.seat_count        = seats;
            reservation.first_seat_number = seatsTaken + 1;
            reservation.reserved_at       = DateTime.Now;

            db.reservations.Add(reservation);
            db.SaveChanges();

            return(Ok(reservation));
        }