From cb0ba494095a6c997a827e430437b5c24377b0c2 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sun, 13 Feb 2022 20:35:52 +0330 Subject: [PATCH] docs: #275 calling upload image api --- GanjooRazor/Pages/Photos.cshtml | 10 ++++- GanjooRazor/Pages/Photos.cshtml.cs | 40 +++++++++++++++++++ .../PoetPhotoSuggestionService.cs | 3 +- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/GanjooRazor/Pages/Photos.cshtml b/GanjooRazor/Pages/Photos.cshtml index 8775aa37..57495623 100644 --- a/GanjooRazor/Pages/Photos.cshtml +++ b/GanjooRazor/Pages/Photos.cshtml @@ -80,7 +80,13 @@ -
+ @if(!string.IsNullOrEmpty(Model.LastError)) + { + @Model.LastError + } + else + { +

@if (Model.Poet == null) { @@ -297,6 +303,8 @@

+ } + \ No newline at end of file diff --git a/GanjooRazor/Pages/Photos.cshtml.cs b/GanjooRazor/Pages/Photos.cshtml.cs index 01a720c6..fc0d573d 100644 --- a/GanjooRazor/Pages/Photos.cshtml.cs +++ b/GanjooRazor/Pages/Photos.cshtml.cs @@ -7,6 +7,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RMuseum.Models.Ganjoor.ViewModels; using System.Collections.Generic; +using System.IO; using System.Net; using System.Net.Http; using System.Text; @@ -150,6 +151,45 @@ namespace GanjooRazor.Pages }; } + public async Task OnPostAsync(PoetPhotoSuggestionUploadModel Upload) + { + LastError = ""; + using (HttpClient secureClient = new HttpClient()) + { + if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) + { + MultipartFormDataContent form = new MultipartFormDataContent(); + + using (MemoryStream stream = new MemoryStream()) + { + form.Add(new StringContent(Upload.PoetId.ToString()), "poetId"); + form.Add(new StringContent(Upload.Title), "title"); + form.Add(new StringContent(Upload.Description), "description"); + form.Add(new StringContent(""), "srcUrl"); + + + await Upload.Image.CopyToAsync(stream); + var fileContent = stream.ToArray(); + form.Add(new ByteArrayContent(fileContent, 0, fileContent.Length), Upload.Image.FileName, Upload.Image.FileName); + + HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/poetphotos", form); + if (!response.IsSuccessStatusCode) + { + LastError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + } + } + } + else + { + LastError = "لطفا از گنجور خارج و مجددا به آن وارد شوید."; + } + + } + + + return Page(); + } + public PhotosModel(HttpClient httpClient) : base(httpClient) { diff --git a/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs b/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs index ab66f33c..985d0b89 100644 --- a/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs +++ b/RMuseum/Services/Implementation/PoetPhotoSuggestionService.cs @@ -72,7 +72,6 @@ namespace RMuseum.Services.Implementation { return new RServiceResult(null, imageRes.ExceptionString); } - await _context.SaveChangesAsync(); try { var image = imageRes.Result; @@ -83,7 +82,7 @@ namespace RMuseum.Services.Implementation { PoetId = poetId, PicOrder = picOrder, - PictureId = image.Id, + Picture = image, SuggestedById = userId, Published = false, ChosenOne = false