public HttpResponseMessage PostComment(int tripId, Comment comment) { using (var repo = new TripRepo()) { comment.Published = DateTime.Now; string usrId = User.Identity.GetUserId(); var usr = repo.Context.Users.FirstOrDefault(x => x.Id == usrId); if (usr != null) { comment.Author = usr; } else { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } Comment res = repo.AddComment(tripId, comment); var response = Request.CreateResponse <CommentDTO>(HttpStatusCode.Created, new CommentDTO(res)); string uri = Url.Link("GetTripCommentById", new { tripId = tripId, id = res.Id }); response.Headers.Location = new Uri(uri); return(response); } }
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 IEnumerable <TripDTO> GetTrips() { using (var repo = new TripRepo()) { var trips = repo.GetAll().Where(x => (!x.IsPrivate || x.Author.Id == User.Identity.GetUserId())).ToList(); var tripsDTO = from trip in trips select new TripDTO(trip); return(tripsDTO.ToList()); } }
public IActionResult Trip(string tag) { var trip = TripRepo.GetAllTrips().Where(t => t.TripTag == tag).FirstOrDefault(); var tripItems = TripItemRepo.GetTripItems().Where(ti => ti.TripTag == trip.TripTag).OrderBy(t => t.PublishDate); return(View("Trip", new ViewModels.Trip { TripTag = trip.TripTag, Title = trip.Title, Image = trip.Image, TripHeader = trip.TripHeader, TripDescription = GetTripItemContent(trip.TripDescription), TripItems = tripItems })); }
public HttpResponseMessage PostTrip(Trip item) { using (var repo = new TripRepo()) { string id = User.Identity.GetUserId(); var usr = repo.Context.Users.FirstOrDefault(x => x.Id == id); item.Author = usr; Trip res = repo.Add(item); var response = Request.CreateResponse <TripDTO>(HttpStatusCode.Created, new TripDTO(res)); string uri = Url.Link("DefaultApi", new { Id = res.Id }); response.Headers.Location = new Uri(uri); return(response); } }
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 IActionResult Trips() { var vm = new List <ViewModels.Trip>(); foreach (var trip in TripRepo.GetAllTrips().OrderBy(t => t.TripOrder)) { vm.Add(new ViewModels.Trip { TripTag = trip.TripTag, Title = trip.Title, Image = trip.Image, TripHeader = trip.TripHeader, TripDescription = GetTripItemContent(trip.TripDescription) }); } return(View("Trips", vm)); }
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); } } }
public IActionResult Index() { var trips = TripRepo.GetAllTrips().OrderBy(t => t.TripOrder); var tripItems = TripItemRepo.GetTripItems().OrderByDescending(t => t.PublishDate); return(View(new Home { Carousels = trips, TripItems = tripItems.Select(ti => new HomeTripItem { Tag = ti.Tag, TripTag = ti.TripTag, HeaderImage = ti.HeaderImage, PublishDate = ti.PublishDate, SubTitle = ti.SubTitle, Title = ti.Title, TripTitle = trips .Where(t => t.TripTag == ti.TripTag) .OrderBy(t => t.TripOrder) .Select(t => t.Title) .FirstOrDefault() }) })); }