public HttpResponseMessage Post(MovieImportRequestModel requestModel)
        {
            string username = User.Identity.Name;
            var user = _membershipService.GetUser(username);

            var dbMovie = _movieService.GetMovie(requestModel.FileHash);
            if (dbMovie == null)
            {
                var movie = Mapper.Map<Movie>(requestModel);
                var createdMovie = _movieService.AddMovie(movie);
                if (!createdMovie.IsSuccess)
                {
                    Log.ErrorFormat("Could not create movie {0} [{1}].  Hashcode was {2}.", requestModel.Title, requestModel.FullPath, requestModel.FileHash);
                    return new HttpResponseMessage(HttpStatusCode.Conflict);
                }
                dbMovie = createdMovie.Entity;
            }

            var dbUserMovie = _movieService.GetUserMovie(dbMovie.Key, user.User.Key);

            if (dbUserMovie == null)
            {
                var userMovie = Mapper.Map<UserMovie>(requestModel);
                userMovie.OwnerKey = user.User.Key;
                userMovie.MovieKey = dbMovie.Key;

                var created = _movieService.AddUserMovie(userMovie);
                if (!created.IsSuccess)
                {
                    Log.ErrorFormat("Could not add movie {0} to user {1}.  Movie ID: {2}", requestModel.Title,
                                    user.User.Key, userMovie.MovieKey);
                    Log.ErrorFormat("Imported File Path: {0}", requestModel.FullPath);
                    Log.ErrorFormat("Imported File Hash: {0}", requestModel.FileHash);

                    var existingUserMovie = dbMovie.UserMovies.FirstOrDefault(x => x.OwnerKey == user.User.Key);
                    if (existingUserMovie != null)
                    {
                        Log.ErrorFormat("Existing File Path: {0}", existingUserMovie.FullPath);
                        Log.ErrorFormat("Existing File Hash: {0}", dbMovie.FileHash);
                    }
                    else
                    {
                        Log.Error("No existing movie. BOGUS ERROR!!!!");
                    }
                    return new HttpResponseMessage(HttpStatusCode.Conflict);
                }
                dbUserMovie = created.Entity;
            }
            else if (requestModel.UpdateExisting)
            {
                dbUserMovie.Filename = requestModel.Filename;
                dbUserMovie.FullPath = requestModel.FullPath;

                _movieService.UpdateUserMovie(dbUserMovie);
            }

            var response = Request.CreateResponse(HttpStatusCode.Created, Mapper.Map<MovieDto>(dbUserMovie));

            return response;
        }
Exemple #2
0
        private static MovieImportRequestModel BuildMovieImportModel(string filename)
        {
            FileInfo fi = new FileInfo(filename);
            MovieImportRequestModel model = new MovieImportRequestModel
                {
                    FileHash = FileHash.ComputeMovieHash(filename),
                    Filename = Path.GetFileName(filename),
                    Format = DetectType.FindVideoSource(filename),
                    FullPath = filename,
                    Title = MovieNaming.GetMovieName(filename, AddFolderType.NameByFile),
                    Year = MovieNaming.GetMovieYear(Path.GetFileName(filename)),
                    FileSize = fi.Length
                };

            return model;
        }
Exemple #3
0
        private static void PostMovie(MovieImportRequestModel model)
        {
            var request = HttpRequestMessageHelper.ConstructRequest(HttpMethod.Post, _movieEndpoint, "application/json",
                                                                    _username, _password);
            request.Content = new ObjectContent<MovieImportRequestModel>(model, new JsonMediaTypeFormatter());

            var response = GetResponse(request);
            if(!response.IsSuccessStatusCode)
            {
                LogMessageFormat("Received status {0} for movie {1}", response.StatusCode, model.Title);
            }
            else
            {
                LogMessageFormat("Posted movie {0} successfully!", model.Title);
            }
        }