public async Task <IActionResult> OnPostAsync(GanjoorGeoLocation Location) { LastMessage = ""; using (HttpClient secureClient = new HttpClient()) { if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/locations", new StringContent(JsonConvert.SerializeObject(Location), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync()); } else { await ReadLocationsAsync(); } } else { LastMessage = "لطفا از گنجور خارج و مجددا به آن وارد شوید."; } } return(Page()); }
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(new BadRequestObjectResult(JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync()))); } } else { return(new BadRequestObjectResult("لطفا از گنجور خارج و مجددا به آن وارد شوید.")); } } return(new JsonResult(true)); }
/// <summary> /// add new location /// </summary> /// <param name="name"></param> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <returns></returns> public async Task <RServiceResult <GanjoorGeoLocation> > AddLocationAsync(string name, double latitude, double longitude) { try { name = name == null ? "" : name.Trim(); if (string.IsNullOrEmpty(name)) { return(new RServiceResult <GanjoorGeoLocation>(null, "نام اجباری است.")); } if (null != await _context.GanjoorGeoLocations.Where(l => l.Name == name).FirstOrDefaultAsync()) { return(new RServiceResult <GanjoorGeoLocation>(null, "نام تکراری است.")); } if (null != await _context.GanjoorGeoLocations.Where(l => l.Latitude == latitude && l.Longitude == longitude).FirstOrDefaultAsync()) { return(new RServiceResult <GanjoorGeoLocation>(null, "مختصات تکراری است.")); } GanjoorGeoLocation location = new GanjoorGeoLocation() { Name = name, Latitude = latitude, Longitude = longitude }; _context.GanjoorGeoLocations.Add(location); await _context.SaveChangesAsync(); return(new RServiceResult <GanjoorGeoLocation>(location)); } catch (Exception exp) { return(new RServiceResult <GanjoorGeoLocation>(null, exp.ToString())); } }
public async Task <IActionResult> UpdateLocationAsync([FromBody] GanjoorGeoLocation location) { if (ReadOnlyMode) { return(BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.")); } var res = await _locationService.UpdateLocationAsync(location); if (!string.IsNullOrEmpty(res.ExceptionString)) { return(BadRequest(res.ExceptionString)); } return(Ok(res.Result)); }
/// <summary> /// update an existing location /// </summary> /// <param name="updated"></param> /// <returns></returns> public async Task <RServiceResult <bool> > UpdateLocationAsync(GanjoorGeoLocation updated) { try { 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; location.Longitude = updated.Longitude; _context.Update(location); await _context.SaveChangesAsync(); return(new RServiceResult <bool>(true)); } catch (Exception exp) { return(new RServiceResult <bool>(false, exp.ToString())); } }