public async Task <HttpResponseMessage> PostPhotoAsync(int tripId) { using (var repo = new TripRepo()) { string usrId = User.Identity.GetUserId(); var usr = repo.Context.Users.FirstOrDefault(x => x.Id == usrId); Trip trip = repo.Get(tripId); if (trip.Author.Id != usrId) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } // Check whether the POST operation is MultiPart? if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } try { // Read all contents of multipart message into MultipartMemoryStreamProvider. MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); List <Photo> tripPhotos = new List <Photo>(); foreach (var file in provider.Contents) { Stream fileStream = await file.ReadAsStreamAsync(); Image img = new Bitmap(fileStream); img = ImageHelper.ResizeImage(img, 1024, 768); string fileName = _fileNameProvider.GetNewFileName(".jpg"); ImageHelper.SaveJpeg(_fileNameProvider.FileSaveLocation + fileName, img, 70); Photo photo = new Photo() { Author = usr, Published = DateTime.Now, ImagePath = fileName }; tripPhotos.Add(photo); } repo.AddPhotos(tripId, tripPhotos); // Send OK Response along with saved file names to the client. return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } } }
public IHttpActionResult GetTrip(int id) { using (var repo = new TripRepo()) { Trip res = repo.Get(id); if (res == null) { return(NotFound()); } if (res.IsPrivate && res.Author.Id != User.Identity.GetUserId()) { return(Unauthorized(null)); } return(Ok(new TripDTO(res))); } }
public void PutTrip(int id, Trip item) { using (var repo = new TripRepo()) { if (repo.Get(id).Author.Id != User.Identity.GetUserId()) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } item.Id = id; if (!repo.Update(item)) { throw new HttpResponseException(HttpStatusCode.NotFound); } } }
public HttpResponseMessage DeleteTrip(int id) { using (var repo = new TripRepo()) { Trip item = repo.Get(id); if (item == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (item.Author.Id != User.Identity.GetUserId()) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } repo.Remove(id); return(Request.CreateResponse(HttpStatusCode.OK)); } }
public CommentDTO GetComment(int tripId, int id) { using (var repo = new TripRepo()) { Trip trip = repo.Get(tripId); if (trip == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } Comment comment = trip.Comments.Where(x => x.Id == id).FirstOrDefault(); if (comment != null) { return(new CommentDTO(comment)); } else { throw new HttpResponseException(HttpStatusCode.NotFound); } } }