public async Task <ActionResult <CuriosityLike> > PostCuriosityLike(int curiosityId) { var curiosityLike = await _context.CuriosityLikes.Where(l => l.CuriosityId == curiosityId).FirstOrDefaultAsync(); if (curiosityLike == null) { var newCuriosityLike = new CuriosityLike() { CuriosityId = curiosityId, Likes = 1 }; _context.CuriosityLikes.Add(newCuriosityLike); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCuriosityLike", new { curiosityId = newCuriosityLike.CuriosityId }, newCuriosityLike)); } else { curiosityLike.Likes += 1; _context.CuriosityLikes.Update(curiosityLike); await _context.SaveChangesAsync(); return(Ok(curiosityLike)); } }
public async Task <IActionResult> PutComment(int id, [FromForm] Comment comment) { if (id != comment.Id) { return(BadRequest()); } _context.Entry(comment).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CommentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Photo> > PostPhoto([FromForm] PhotosPostDTO model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //string uniqueFileName = ProcessFoto(model); string uniqueFileName = await _AzureFileService.ProcessPhotoGalleryForCuriosityAsync(model); Photo photo = new Photo() { CuriosityId = model.CuriosityId, PhotoPath = uniqueFileName }; _context.Photos.Add(photo); await _context.SaveChangesAsync(); var photoDTO = await ProcessPhotoDTO(photo); return(CreatedAtAction("GetPhoto", new { id = photo.Id }, photoDTO)); //return Ok(newMmtFoto); //return CreatedAtAction("GetPhoto", new { CuiosityId = photo.CuriosityId }, photo); //_context.Photos.Add(photo); //await _context.SaveChangesAsync(); //return CreatedAtAction("GetPhoto", new { id = photo.Id }, photo); }
public async Task <IActionResult> ManageCuriositiesForTourWith(int tourId, [FromForm] int[] CuriositiesIds = null) { var tour = await _context.Tours.FirstOrDefaultAsync(t => t.Id == tourId); if (tour == null) { ModelState.AddModelError("", $"tour with id : {tourId} not found"); return(NotFound(ModelState)); } if (CuriositiesIds == null) { ModelState.AddModelError("", $"curiosities Ids cannot be null"); return(BadRequest(ModelState)); } List <TourCuriosity> tourCuriosities = new List <TourCuriosity>(); foreach (var curiosityId in CuriositiesIds) { if (!CuriosityExists(curiosityId)) { ModelState.AddModelError("", $"the curiosity with id ${curiosityId} not found"); } tourCuriosities.Add(new TourCuriosity { Tour = tour, Curiosity = (await _context.Curiosities.FirstOrDefaultAsync(c => c.Id == curiosityId)) }); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var oldTourCuriosities = await _context.tourCuriosities.Where(tc => tc.TourId == tourId).ToListAsync(); if (oldTourCuriosities != null) { _context.tourCuriosities.RemoveRange(oldTourCuriosities); } await _context.tourCuriosities.AddRangeAsync(tourCuriosities); try { await _context.SaveChangesAsync(); } catch (Exception e) { return(BadRequest(e)); } return(Ok(new { tour, CuriositiesIds })); }
public async Task <IActionResult> PutTour(int id, [FromForm] TourPutDTO model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var tour = await _context.Tours.FindAsync(id); if (tour == null) { ModelState.AddModelError("", $"Tour with id : {id} not found"); return(NotFound(ModelState)); } if (model.Image != null) { if (tour.Image != null) { //string filePath = Path.Combine(webHostEnvironment.WebRootPath, // "TourImages", tour.Image); //System.IO.File.Delete(filePath); await _AzureFileService.DeleteTourImageAsync(tour.Image); } //tour.Image = ProcessUploadedFile(model); tour.Image = await _AzureFileService.ProcessTourImgAsync(model); } tour.City = model.City; tour.Coordinates = model.Coordinates; tour.Country = model.Country; tour.Description = model.Description; tour.Latitude = model.Latitude; tour.Longitude = model.Longitude; tour.Name = model.Name; tour.Province = model.Province; tour.Region = model.Region; tour.Type = model.Type; _context.Entry(tour).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TourExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCuriosity(int id, [FromForm] CuriositiesPutDTO model) { if (!ModelState.IsValid) { return(BadRequest()); } var curiosity = await _context.Curiosities.FindAsync(id); if (curiosity == null) { ModelState.AddModelError("", $"Curiosity with id : ${id} not found"); return(NotFound(ModelState)); } string uniqueFileName = ""; if (model.Image != null) { if (curiosity.Image != null) { await _AzureFileService.DeleteCuriosityImageAsync(curiosity.Image); //string filePath = Path.Combine(webHostEnvironment.WebRootPath, // "images", curiosity.Image); //System.IO.File.Delete(filePath); } //if (model.ExistingImage != null) //{ // string filePath = Path.Combine(webHostEnvironment.WebRootPath, // "images", model.ExistingImage); // System.IO.File.Delete(filePath); //} //curiosity.Image = ProcessUploadedFile(model); uniqueFileName = await _AzureFileService.ProcessFotoAsync(model); curiosity.Image = uniqueFileName; } curiosity.City = model.City; curiosity.Coordinates = model.Coordinates; curiosity.Country = model.Country; curiosity.Description = model.Description; curiosity.Latitude = model.Latitude; curiosity.Longitude = model.Longitude; curiosity.Name = model.Name; curiosity.Period = model.Period; curiosity.Province = model.Province; curiosity.Region = model.Region; curiosity.Type = model.Type; //_context.Update(curiosity); _context.Entry(curiosity).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CuriosityExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }