Ejemplo n.º 1
0
        public async Task <IActionResult> CreateOrUpdate([FromForm] CreateOrUpdateActorViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            await PrepareAsync(model);

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        private async Task <IEnumerable <FilmReturnModel> > FetchSelectedFilmsAsync(CreateOrUpdateActorViewModel model)
        {
            var actors = new List <FilmReturnModel>();

            if (model.IsNopeFilms)
            {
                return(actors);
            }
            if (model.SelectedFilms == null)
            {
                return(actors);
            }

            foreach (var selectedFilmId in model.SelectedFilms)
            {
                actors.Add(await _apiTools.FetchAsync <FilmReturnModel>(
                               $"https://localhost:5001/api/films/get/{selectedFilmId}"));
            }

            return(actors);
        }
Ejemplo n.º 3
0
        private async Task PrepareAsync(CreateOrUpdateActorViewModel model)
        {
            var selectedFilms = await FetchSelectedFilmsAsync(model);

            if (model.Actor.ActorId == 0)
            {
                await _apiTools.PostAsync <ActorReturnModel>("https://localhost:5001/api/actors/create",
                                                             new { model.Actor.FirstName, model.Actor.LastName, model.Actor.Birthday, Films = selectedFilms });
            }
            else
            {
                await _apiTools.PostAsync <ActorReturnModel>("https://localhost:5001/api/actors/update",
                                                             new
                {
                    model.Actor.ActorId,
                    model.Actor.FirstName,
                    model.Actor.LastName,
                    model.Actor.Birthday,
                    Films = selectedFilms
                });
            }
        }