示例#1
0
        public async Task <IActionResult> Details(int id)
        {
            ActorVM actor;

            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:55169/api/Actors/" + id);

            request.Headers.Add("Accept", "application/json");

            var client = _httpClientFactory.CreateClient();

            var response = await client.SendAsync(request).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                actor = await JsonSerializer.DeserializeAsync <ActorVM>(responseStream);
            }
            else
            {
                actor = new ActorVM();
            }

            return(View(actor));
        }
示例#2
0
        public async Task <IActionResult> Create(ActorVM actor)
        {
            if (ModelState.IsValid)
            {
                var client = _httpClientFactory.CreateClient("MMDB_API");

                string uniqueFileName = UploadedFile(actor);

                ActorPutVM actorPost = new ActorPutVM
                {
                    Name        = actor.Name,
                    DateOfBirth = actor.DateOfBirth,
                    gender      = actor.gender,
                    Avatar      = uniqueFileName
                };

                var actorContent = new StringContent(JsonSerializer.Serialize(actorPost), Encoding.UTF8, "application/json");

                var response = await client.PostAsync("actors", actorContent).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    return(RedirectToAction(nameof(Index)));
                }

                return(RedirectOnError(response, "Create", "create an actor", "creating an actor"));
            }

            return(View(actor));
        }
示例#3
0
        public ActionResult UpdateActor(int id)
        {
            ActorVM avm = new ActorVM
            {
                Actor = _acRep.Find(id),
            };

            return(View(avm));
        }
示例#4
0
        public ActionResult ActorList()
        {
            ActorVM avm = new ActorVM
            {
                Actors = _acRep.GetActives(),
            };

            return(View(avm));
        }
示例#5
0
        public ActionResult AddActor()
        {
            ActorVM avm = new ActorVM
            {
                Actor  = new Actor(),
                Movies = _mRep.GetActives(),
            };

            return(View(avm));
        }
示例#6
0
        public ActionResult UpdateActor(ActorVM item)
        {
            Actor toBeUpdated = _acRep.FirstOrDefault(x => x.ID == item.Actor.ID);

            toBeUpdated.FirstName = item.Actor.FirstName;
            toBeUpdated.LastName  = item.Actor.LastName;
            toBeUpdated.Age       = item.Actor.Age;
            toBeUpdated.Country   = item.Actor.Country;

            _acRep.Update(toBeUpdated);


            return(RedirectToAction("ActorList"));
        }
示例#7
0
        private string UploadedFile(ActorVM actor)
        {
            string uniqueFileName = null;

            if (actor.AvatarFile != null)
            {
                string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images/Avatars");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + actor.AvatarFile.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    actor.AvatarFile.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
示例#8
0
        public async Task <IActionResult> Delete(int id, IFormCollection collection)
        {
            var client = _httpClientFactory.CreateClient("MMDB_API");

            var response = await client.GetAsync("actors/" + id).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                ActorVM actor = await JsonSerializer.DeserializeAsync <ActorVM>(responseStream);

                return(View(actor));
            }

            return(RedirectOnError(response, "Delete", "delete an actor", "getting the actor details"));
        }
示例#9
0
        public ActorVM Insert(ActorInsertRequest request)
        {
            ActorVM viewModel = new ActorVM();

            if (request == null)
            {
                return(viewModel);
            }

            Actors actor = new Actors();

            actor.Name = request.Name;
            actor.Age  = request.Age;

            _context.Actors.Add(actor);
            _context.SaveChanges();

            return(_mapper.Map <ActorVM>(actor));
        }