Esempio n. 1
0
        public void Put(Review review)
        {
            if (review == null)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            if (!_usersController.IsUserValid(Request))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            ReviewData serverData = repository.Get(review.id);
            if (serverData == null) //trying to "update" an object that doesn't exist.
            {
                throw new HttpResponseException(HttpStatusCode.NotAcceptable);
            }

            ReviewData reviewData = AutoMapper.Mapper.Map<Review, ReviewData>(review);

            if (reviewData.comments.Count == serverData.comments.Count && reviewData.user == _usersController.GetUserIdFromHeaders(Request))
                reviewData.lastModified = DateTime.UtcNow; //review updated, not just added a comment - and the user is the updater...

            repository.Update(reviewData);
        }
Esempio n. 2
0
        public HttpResponseMessage Post(Review review)
        {
            if (!_usersController.IsUserValid(Request))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            ReviewData check = repository.Get(review.id);
            if (check != null)
            {
                throw new HttpResponseException(HttpStatusCode.MethodNotAllowed);
            }
            ReviewData reviewData = AutoMapper.Mapper.Map<Review, ReviewData>(review);
            reviewData = repository.Add(reviewData);
            if (reviewData == null)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            Review output = PopulateData(reviewData);

            var response = Request.CreateResponse<Review>(HttpStatusCode.Created, output);

            string uri = Url.Link("DefaultApi", new { id = output.id });
            response.Headers.Location = new Uri(uri);
            return response;
        }