Example #1
0
        public IHttpActionResult GetPlace([FromUri] string placeId)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest());
            }

            if (!Guid.TryParse(placeId, out var modelPlaceId))
            {
                return(this.BadRequest());
            }

            Models.Place.Place modelPlace = null;

            try
            {
                modelPlace = this.repository.Get(modelPlaceId);
            }
            catch (Models.Place.PlaceNotFoundException)
            {
                return(this.NotFound());
            }

            return(this.Ok(PlaceConverter.Convert(modelPlace)));
        }
Example #2
0
        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)));
        }