public IHttpActionResult PatchPlace([FromUri] string placeId, [FromBody] PlacePatchInfo patchInfo) { if (!ModelState.IsValid) { return(this.BadRequest()); } if (!Guid.TryParse(placeId, out var placeIdGuid)) { return(this.BadRequest()); } string sessionId = ""; CookieHeaderValue cookie = Request.Headers.GetCookies("SessionId").FirstOrDefault(); if (cookie != null) { sessionId = cookie["SessionId"].Value; } if (!authenticator.TryGetSession(sessionId, out var sessionState)) { return(this.Unauthorized()); } try { var place = this.repository.Get(placeIdGuid); if (sessionState.UserId != place.OwnerId) { return(this.Unauthorized()); } } catch (Exception) { return(this.Unauthorized()); } var placePatchInfo = PlacePatchInfoConverter.Convert(placeIdGuid, patchInfo); Models.Place.Place modelPlace = null; try { modelPlace = this.repository.Patch(placePatchInfo); } catch (Models.Place.PlaceNotFoundException) { this.NotFound(); } return(Ok(PlaceConverter.Convert(modelPlace))); }
public IHttpActionResult PatchPlace([FromUri] string placeId, [FromBody] PlacePatchInfo patchInfo) { string sessionId = patchInfo.SessionId; if (!authenticator.TryGetSession(sessionId, out var sessionState)) { return(this.Unauthorized()); } if (!Guid.TryParse(placeId, out var placeIdGuid)) { return(this.BadRequest()); } if (!ModelState.IsValid) { return(this.BadRequest(ModelState)); } if (patchInfo == null) { return(this.BadRequest("Body must be not null")); } try { var place = this.repository.Get(placeIdGuid); if (sessionState.UserId != place.OwnerId) { return(this.Unauthorized()); } } catch (Exception) { return(this.Unauthorized()); } var placePatchInfo = PlacePatchInfoConverter.Convert(placeIdGuid, patchInfo); Models.Place.Place modelPlace = null; try { modelPlace = this.repository.Patch(placePatchInfo); } catch (Models.Place.PlaceNotFoundException) { this.NotFound(); } return(Ok(PlaceConverter.Convert(modelPlace))); }
public Place Patch(PlacePatchInfo patchInfo) { if (patchInfo == null) { throw new ArgumentNullException(nameof(patchInfo)); } var place = Get(patchInfo.PlaceId); if (patchInfo.Name != null) { place.Name = patchInfo.Name; } if (patchInfo.Address != null) { place.Address = patchInfo.Address; } places.ReplaceOne(x => x.Id == place.Id, place); return(place); }
/// <summary> /// Изменить передержку /// </summary> /// <param name="patchInfo">Описание изменений передержки</param> /// <returns>Измененная передержка</returns> public Place Patch(PlacePatchInfo patchInfo) { if (patchInfo == null) { throw new ArgumentNullException(nameof(patchInfo)); } if (!primaryKeyIndex.TryGetValue(patchInfo.PlaceId, out var place)) { throw new PlaceNotFoundException(patchInfo.PlaceId); } if (patchInfo.Name != null) { place.Name = patchInfo.Name; } if (patchInfo.Address != null) { place.Address = patchInfo.Address; } return(place); }