Пример #1
0
        public async Task <ActionResult <ActorModel> > Post(ActorModel model)
        {
            try
            {
                if (model.Films != null && model.Films.Count() > 0)
                {
                    return(BadRequest("Add actor first. If you want to add films update actor"));
                }

                var existingActor = await _repository.GetActorByNameAsync(model.FirstName, model.LastName);

                if (existingActor != null)
                {
                    return(BadRequest("The actor already exists"));
                }

                var actor = _mapper.Map <Actor>(model);

                _repository.Add(actor);

                if (await _repository.SaveChangesAsync())
                {
                    actor = await _repository.GetActorByNameAsync(actor.FirstName, actor.LastName);

                    return(Created($"api/Actors/{model.FirstName}/{model.LastName}", _mapper.Map <ActorModel>(actor)));
                }
                return(StatusCode(StatusCodes.Status500InternalServerError, "It looks like no changes were made"));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }
Пример #2
0
        //  [ValidateAntiForgeryToken]
        public ActionResult Create(FilmViewModel model)
        {
            var imageTypes = new string[] {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };

            //  if (model.Image == null || model.Image.ContentLength == 0)
            //   {
            //      ModelState.AddModelError("ImageUpload", "Додайте зображення");
            //   }
            if (model.Image != null && !imageTypes.Contains(model.Image.ContentType))
            {
                ModelState.AddModelError("Image", "Зображення повинне бути у GIF, JPG або PNG форматі.");
            }
            else if (model.CategoryId == null)
            {
                ModelState.AddModelError("CategoryId", "Додайте жанр");
            }

            if (ModelState.IsValid)
            {
                var film = new Film();
                film.Name        = model.Name;
                film.Description = model.Description;
                if (model.CategoryId != null)
                {
                    foreach (var c in model.CategoryId)
                    {
                        if (repo.Categories.Find(m => m.Id == c) != null)
                        {
                            film.Categories.Add(repo.Categories.Find(m => m.Id == c));
                        }
                    }
                }
                if (model.Image != null)
                {
                    byte[] imageData    = null;
                    var    binaryReader = new BinaryReader(model.Image.OpenReadStream());

                    imageData = binaryReader.ReadBytes((int)model.Image.Length);
                    binaryReader.Close();
                    MemoryStream ms = new MemoryStream(imageData);
                    ms.Close();
                    ViewBag.Width  = 0;
                    ViewBag.Height = 0;
                    film.Image     = imageData;
                }
                repo.Add(film);
                repo.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.Categories = repo.Categories;
            return(View(model));
        }
Пример #3
0
        public async Task <Film> Create(CreateFilm data,
                                        [Service] IFilmRepository repository,
                                        [Service] ICountryRepository countries)
        {
            var country = await countries.GetByIso(data.Country);

            var film = new Film(data.Name, data.ReleaseYear, country);
            await repository.Add(film);

            return(film);
        }
Пример #4
0
        public async Task <ActionResult> Add([FromBody] Film film)
        {
            _logger.LogInformation("FilmsController::Add");

            var saved = await repository.Add(film);

            if (saved)
            {
                return(RedirectToAction(nameof(Get), new { title = film.Title }));
            }

            return(Ok(new { Status = "Error", Message = "the object could not be saved" }));
        }
Пример #5
0
        //public IHttpActionResult PostFilm(Film film)
        public IHttpActionResult PostFilm(FilmDTO filmDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var film = Mapper.Map <Film>(filmDTO);

            _filmRepository.Add(film);

            //db.Filmovi.Add(film);
            //db.SaveChanges();

            //db.Entry(film).Reference(x => x.Reziser).Load();
            return(CreatedAtRoute("DefaultApi", new { id = film.Id }, film));
        }
Пример #6
0
 public void AddFilm(string name, string description,
                     DateTime releaseDate, int duration, string director, string image)
 {
     /* Guid userIdGuid = Guid.Empty;
      * if (!Guid.TryParse(userId, out userIdGuid))
      * {
      *   throw new Exception("Invalid Guid Format");
      * }
      * var admin = adminRepository.GetAdminById(userIdGuid);
      * if (admin == null)
      * {
      *   throw new EntityNotFoundException(userIdGuid);
      * }*/
     filmRepository.Add(new Film()
     {
         Name     = name, Description = description,
         Director = director, Duration = duration, ReleaseDate = releaseDate, ImageByteArray = image
     });
 }
        public ResultModel <Film> FilmSave(Film model)
        {
            FilmKayitValidator validator = new FilmKayitValidator();
            ValidationResult   result    = validator.Validate(model);

            if (result.IsValid)
            {
                _filmRepository.Add(model);
                return(new ResultModel <Film>
                {
                    Errors = null,
                    IsValid = true,
                    Message = "Kayıt Başarılı"
                });
            }
            return(new ResultModel <Film>
            {
                Errors = result.Errors.Select(x => x.ErrorMessage).ToList(),
                IsValid = false,
                Message = "Kayıt Başarısız"
            });
        }
Пример #8
0
        public async Task <ActionResult <DirectorModel> > Post(DirectorModel model)
        {
            try
            {
                if (model.Films != null && model.Films.Count() > 0)
                {
                    return(BadRequest("Please add director first. Than you can update list of films with PUT request"));
                }

                var filmsModelValidationResult = ModelsValidator.ValidateFilmModels(model);

                if (!filmsModelValidationResult.Item1)
                {
                    return(BadRequest(filmsModelValidationResult.Item2));
                }

                var existingDirector = await _repository.GetDirectorByNameAsync(model.FirstName, model.LastName);

                if (existingDirector != null)
                {
                    return(BadRequest("There is director with the name in db"));
                }

                var director = _mapper.Map <Director>(model);

                _repository.Add(director);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/Directors/{model.FirstName}/{model.LastName}", _mapper.Map <DirectorModel>(director)));
                }

                return(StatusCode(StatusCodes.Status500InternalServerError, "It looks like no changes were made"));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }
Пример #9
0
        public ResultModel <Film> SaveFilm(Film f)
        {
            FilmValidator validator = new FilmValidator();
            var           result    = validator.Validate(f);

            if (result.IsValid)
            {
                _filmRepo.Add(f);
                _filmRepo.Save();

                return(new ResultModel <Film>
                {
                    Errors = null,
                    IsValid = true,
                    Message = "Başarıyla kayıt edildi!"
                });
            }
            return(new ResultModel <Film>
            {
                Errors = result.Errors.Select(x => x.ErrorMessage).ToList(),
                IsValid = false,
                Message = "Kayıt işlemi başarısız!"
            });
        }
Пример #10
0
 public RepositoryResult AddFilm(string title, int year)
 {
     return(_filmRepository.Add(new Film(title, year)));
 }
Пример #11
0
        public void Add(FilmDto filmDto)
        {
            var film = new Film(filmDto.Filme_Id, filmDto.Titulo, filmDto.Nota, filmDto.Lancamento);

            _filmRepository.Add(film);
        }
Пример #12
0
        public async Task <ActionResult <FilmModel> > Post(FilmModel model)
        {
            try
            {
                if (model.Cast != null && model.Cast.Count() > 0)
                {
                    return(BadRequest("Please add film first. Update films cast with updatefilm method"));
                }

                var directorModelValidationResult = ModelsValidator.ValidateDirectorModel(model);

                if (!directorModelValidationResult.Item1)
                {
                    return(BadRequest(directorModelValidationResult.Item2));
                }

                var actorsModelValidationResult = ModelsValidator.ValidateActorModels(model);

                if (!actorsModelValidationResult.Item1)
                {
                    return(BadRequest(actorsModelValidationResult.Item2));
                }

                var film = await _repository.GetFilmByTitleAsync(model.Title);

                if (film != null)
                {
                    return(BadRequest("There is a film with same name"));
                }

                film = _mapper.Map <Film>(model);

                var updateDirectorResult = await UpdateDirectorAsync(film);

                if (!updateDirectorResult.Item1)
                {
                    return(BadRequest(updateDirectorResult.Item2));
                }

                var updateCastResult = await UpdateCastAsync(film);

                if (!updateCastResult.Item1)
                {
                    return(BadRequest(updateCastResult.Item2));
                }

                _repository.Add(film);

                if (await _repository.SaveChangesAsync())
                {
                    film = await _repository.GetFilmByTitleAsync(model.Title, true, true);

                    return(Created($"/api/films/{model.Title}", _mapper.Map <FilmModel>(film)));
                }

                return(StatusCode(StatusCodes.Status500InternalServerError, "It looks like no changes were made"));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }
 private AddFilmResult Add(string title, int year) =>
 _filmRepository.Add(new Film(title, year)) == RepositoryResult.Successful
     ? AddFilmResult.Successful
     : AddFilmResult.Failed;
Пример #14
0
 //Method
 public Film Add(Film film)
 {
     return(_filmRepository.Add(film));
 }
Пример #15
0
        public void Add(FilmViewModel viewModel)
        {
            var film = _mapper.Map <FilmViewModel, FilmEntity>(viewModel);

            _filmeFilmRepository.Add(film);
        }