#233 editing locations UI is now complete

This commit is contained in:
Hamid Reza Mohammadi 2021-10-29 15:32:34 +03:30
parent 7df0673f9f
commit ece77634a8
3 changed files with 121 additions and 13 deletions

View File

@ -42,14 +42,68 @@
</td>
</tr>
</table>
</form>
@if (Model.Locations != null)
{
<script>
function editLocation(itemId) {
var url = '?handler=Edit';
var nameInput = '#name-' + itemId.toString();
var latInput = '#lat-' + itemId.toString();
var lonInput = '#lon-' + itemId.toString();
$.ajax({
type: "PUT",
url: url,
data: {
id: itemId,
name: $(nameInput).val(),
latitude: $(latInput).val(),
longitude: $(lonInput).val(),
},
error: function (e) {
if (e.responseText == null)
alert(e);
else
alert(e.responseText);
},
success: function () {
alert('ردیف به درستی ویرایش شد.')
},
});
}
function deleteLocation(itemId) {
if (!confirm('آیا از حذف این ردیف اطمینان دارید؟'))
return;
$.ajax({
type: "DELETE",
url: '',
data: {
id: itemId
},
error: function (e) {
if (e.responseText == null)
alert(e);
else
alert(e.responseText);
},
success: function () {
var itemIdBlockId = '#tr-' + itemId;
$(itemIdBlockId).remove();
},
});
}
</script>
<table>
<tr>
<th>نام</th>
@ -61,15 +115,15 @@
</tr>
@foreach (var location in Model.Locations)
{
<tr id="tr-@location.Id">
<td>@location.Name</td>
<td>@location.Latitude</td>
<td>@location.Longitude</td>
<td>
<a role="button" onclick="editBanner(@location.Id)" class="actionlink" title="ویرایش">✎</a>
<a role="button" onclick="deleteBanner(@location.Id)" class="actionlink" title="حذف">🗑</a>
</td>
</tr>
<tr id="tr-@location.Id">
<td><input type="text" name="name-@location.Id" id="name-@location.Id" value="@location.Name" /></td>
<td><input type="text" name="lat-@location.Id" id="lat-@location.Id" value="@location.Latitude" /></td>
<td><input type="text" name="lon-@location.Id" id="lon-@location.Id" value="@location.Longitude" /></td>
<td>
<a role="button" onclick="editLocation(@location.Id)" class="actionlink" title="ویرایش">✎</a>
<a role="button" onclick="deleteLocation(@location.Id)" class="actionlink" title="حذف">🗑</a>
</td>
</tr>
}
</table>

View File

@ -75,5 +75,52 @@ namespace GanjooRazor.Areas.Admin.Pages
}
return Page();
}
public async Task<IActionResult> OnDeleteAsync(int id)
{
using (HttpClient secureClient = new HttpClient())
{
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
{
var response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/locations/{id}");
if (!response.IsSuccessStatusCode)
{
return BadRequest(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
}
}
return new JsonResult(true);
}
public async Task<IActionResult> OnPutEditAsync(int id, string name, double latitude, double longitude)
{
using (HttpClient secureClient = new HttpClient())
{
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
{
GanjoorGeoLocation model = new GanjoorGeoLocation()
{
Id = id,
Name = name,
Latitude = latitude,
Longitude = longitude
};
HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/locations", new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
return BadRequest(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
}
else
{
LastMessage = "لطفا از گنجور خارج و مجددا به آن وارد شوید.";
}
}
return new JsonResult(true);
}
}
}

View File

@ -60,6 +60,13 @@ namespace RMuseum.Services.Implementation
var location = await _context.GanjoorGeoLocations.Where(l => l.Id == updated.Id).SingleOrDefaultAsync();
if (location == null)
return new RServiceResult<bool>(false, "اطلاعات مکان یافت نشد.");
updated.Name = updated.Name.Trim();
if (string.IsNullOrEmpty(updated.Name))
return new RServiceResult<bool>(false, "نام اجباری است.");
if (null != await _context.GanjoorGeoLocations.Where(l => l.Name == updated.Name && l.Id != updated.Id).FirstOrDefaultAsync())
return new RServiceResult<bool>(false, "نام تکراری است.");
if (null != await _context.GanjoorGeoLocations.Where(l => l.Latitude == updated.Latitude && l.Longitude == updated.Longitude && l.Id != updated.Id).FirstOrDefaultAsync())
return new RServiceResult<bool>(false, "مختصات تکراری است.");
location.Name = updated.Name;
location.Latitude = updated.Latitude;