コード例 #1
0
ファイル: ActorsController.cs プロジェクト: FlorVdK/MMDB-API
        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));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id)
        {
            ActorPutVM 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 <ActorPutVM>(responseStream);
            }
            else
            {
                actor = new ActorPutVM();
            }

            return(View(actor));
        }
コード例 #3
0
ファイル: ActorsController.cs プロジェクト: FlorVdK/MMDB-API
        public async Task <IActionResult> Edit(int id, ActorPutVM actor)
        {
            if (ModelState.IsValid)
            {
                var client = _httpClientFactory.CreateClient("MMDB_API");

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

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

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

                return(RedirectOnError(response, "Edit", "edit an actor", "saving the modified actor"));
            }

            return(View(actor));
        }
コード例 #4
0
        public async Task <IActionResult> Edit(int id, ActorPutVM actorPut)
        {
            if (actorPut.MovieActorDTOs == null)
            {
                actorPut.MovieActorDTOs = new MovieActorVM[0];
            }
            if (ModelState.IsValid)
            {
                var client = _httpClientFactory.CreateClient();

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

                HttpResponseMessage httpResponseMessage = await client.PutAsync(new Uri("http://localhost:55169/api/Actors/" + id), actorContent).ConfigureAwait(false);

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

            return(View(actorPut));
        }