private Reviewable GetComputedValues(Reviewable reviewable, Guid userId) { if (userId.Equals(Guid.Empty)) return reviewable; IEnumerable<ReviewData> review = _reviewRepository.GetByIdByOwner(reviewable.id, userId); if (review != null && review.Count() > 0) { reviewable.hasReviewed = true; reviewable.hasReviewedId = review.Last<ReviewData>().id; } else { reviewable.hasReviewed = false; reviewable.hasReviewedId = Guid.Empty; } return reviewable; }
public HttpResponseMessage Post(Reviewable reviewable) { if (!_usersController.IsUserValid(Request)) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } if (reviewable == null) { throw new HttpResponseException(HttpStatusCode.InternalServerError); } //map the reviewable to reviewableData ReviewableData reviewableData = AutoMapper.Mapper.Map<Reviewable, ReviewableData>(reviewable); //first save the images and store the Guid's List<string> ids = new List<string>(); if (reviewable.images != null) { foreach (Image i in reviewable.images) { Guid newImgId = Guid.NewGuid(); i.id = newImgId; //map each image to an imagedata AutoMapper.Mapper.CreateMap<Image, ImageData>(); ImageData imageData = AutoMapper.Mapper.Map<Image, ImageData>(i); //save each imagedata imageData = _imagesRepository.Add(imageData); //store each id in the string array ids.Add(imageData.id.ToString()); } } //add the image ids to the reviewableData string array reviewableData.images = ids.ToArray(); //set the created and lastmodified dates reviewableData.createdDate = DateTime.UtcNow; reviewableData.lastModified = reviewableData.createdDate; //now save the reviewable reviewableData = repository.Add(reviewableData, _usersController.GetUserIdFromHeaders(Request)); //now transform the reviewableData back to a reviewable AutoMapper.Mapper.CreateMap<ReviewableData, Reviewable>(); reviewable = AutoMapper.Mapper.Map<ReviewableData, Reviewable>(reviewableData); //reviewable = CopyImagesfromDomainToUI(reviewableData, reviewable); reviewable = GetComputedValues(reviewable, _usersController.GetUserIdFromHeaders(Request)); var response = Request.CreateResponse<Reviewable>(HttpStatusCode.Created, reviewable); string uri = Url.Link("DefaultApi", new { id = reviewable.id }); response.Headers.Location = new Uri(uri); return response; }