コード例 #1
0
        public async Task <JObject> CreateExerciseAsync(CreateExerciseRequestModel model, string accessToken)
        {
            try
            {
                if (!model.Image.IsImage())
                {
                    JObject e = new();

                    e.Add("statusCode", 400);
                    e.Add("title", "The file must be an image");
                    e.Add("succeeded", false);

                    return(e);
                }


                var content = new MultipartFormDataContent();

                content.Add(new StreamContent(model.Image.OpenReadStream()), "Image", "Image");

                content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "Image", FileName = model.Image.FileName
                };

                content.Add(new StringContent(model.Category), "CategoryId");
                content.Add(new StringContent(model.Name), "Name");
                content.Add(new StringContent(model.FileContentType), "FileContentType");

                var client = _httpClientFactory.CreateClient("Ocelot");

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                var result = await client.PostAsync("/v1/Exercise", content);

                string bodyContent = await result.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(bodyContent))
                {
                    return(JObject.Parse(bodyContent));
                }

                JObject error = new();

                error.Add("statusCode", (int)result.StatusCode);
                error.Add("title", result.ReasonPhrase);
                error.Add("succeeded", false);

                return(error);
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #2
0
        public async Task <IActionResult> CreateExercise(CreateExerciseRequestModel model)
        {
            try
            {
                model.FileContentType = model.Image.ContentType;

                string accessToken = await HttpContext.GetTokenAsync("access_token");

                ViewBag.Result = await _exerciseService.CreateExerciseAsync(model, accessToken);
            }
            catch (Exception)
            {
                JObject error = new();

                error.Add("succeeded", false);
                error.Add("title", "Internal Error");
            }

            return(View());
        }