public async Task <IHttpActionResult> PostSpotz(Spotz spotz) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Spotzes.Add(spotz); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (SpotzExists(spotz.SpotzId)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = spotz.SpotzId }, spotz)); }
public async Task <IHttpActionResult> PutSpotz(Guid id, Spotz spotz) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != spotz.SpotzId) { return(BadRequest()); } db.Entry(spotz).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpotzExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <ActionResult> DeleteConfirmed(Guid id) { Spotz spotz = await _db.Spotzes.FindAsync(id); _db.Spotzes.Remove(spotz); await _db.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IHttpActionResult> FileUpload(string id) { var spotz = await _db.Spotzes.FindAsync(Guid.Parse(id)); if (spotz == null) { //return NotFound(); var newSpotz = new Spotz { SpotzId = Guid.Parse(id), Timestamp = DateTime.Now }; try { _db.Spotzes.Add(newSpotz); } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } } var request = HttpContext.Current.Request; var file = request.Files[0]; var title = request.Form["title"]; var imageId = Guid.NewGuid(); try { var newImage = new Image { Data = _helperMethods.ConvertToByteArray(file.InputStream), Timestamp = DateTime.Now, Filename = title, ImageId = imageId, ImageUrl = Url.Route("Images", new { id = imageId }), Spotz = spotz }; if (spotz != null) { spotz.Images.Add(newImage); _db.Entry(spotz).State = EntityState.Modified; } _db.Images.Add(newImage); _db.SaveChanges(); return(Json(new { status = "complete", imgurl = newImage.ImageUrl })); } catch (Exception e) { Console.WriteLine(e); return(Json(new { status = "error", message = e })); } }
public async Task <IHttpActionResult> GetSpotz(Guid id) { Spotz spotz = await db.Spotzes.FindAsync(id); if (spotz == null) { return(NotFound()); } return(Ok(spotz)); }
public async Task <ActionResult> Edit([Bind(Include = "SpotzId,Title,Description,Longitude,Latitude,Timestamp")] Spotz spotz) { if (ModelState.IsValid) { _db.Entry(spotz).State = EntityState.Modified; await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(spotz)); }
public async Task <ActionResult> Create([Bind(Include = "SpotzId,Title,Description,Longitude,Latitude,Timestamp")] Spotz spotz) { if (ModelState.IsValid) { spotz.SpotzId = Guid.NewGuid(); _db.Spotzes.Add(spotz); await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(spotz)); }
public async Task <IHttpActionResult> DeleteSpotz(Guid id) { Spotz spotz = await db.Spotzes.FindAsync(id); if (spotz == null) { return(NotFound()); } db.Spotzes.Remove(spotz); await db.SaveChangesAsync(); return(Ok(spotz)); }
// GET: Spotz/Edit/5 public async Task <ActionResult> Edit(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Spotz spotz = await _db.Spotzes.FindAsync(id); if (spotz == null) { return(HttpNotFound()); } return(View(spotz)); }
// GET: Spotz/Details/5 public async Task <ActionResult> Details(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Spotz spotz = await _db.Spotzes.FindAsync(id); if (spotz == null) { return(HttpNotFound()); } var vm = new SpotzDetailViewModel(spotz); return(View(vm)); }
public async Task <IHttpActionResult> AddComment(AddedComment addedComment) { if (addedComment == null) { return(NotFound()); } if (addedComment.Comment == null) { return(Json(new { status = "error", message = "No commment added!" })); //return await HttpResponseMessage(HttpStatusCode.InternalServerError); } var idGuid = Guid.Parse(addedComment.Id); Spotz spotz = await db.Spotzes.FindAsync(idGuid); var newComment = new Comment { Timestamp = DateTime.Now, User = db.Users.Find(addedComment.UserId), CommentId = Guid.NewGuid(), Text = addedComment.Comment, Spotz = spotz }; try { spotz?.Comments.Add(newComment); await db.SaveChangesAsync(); } catch (Exception) { return(Json(new { status = "error", message = "Error creating comment!" })); } return(Json(new { status = "success", message = "Comment created!", comment = addedComment.Comment, user = newComment.User.UserName, gravatarurl = newComment.User.GravatarUrl })); //return Json(newComment); }
// Lagrer ny spotz public Spotz AddSpotz(AddSpotzViewModel vm, HttpContext httpContext) { var user = _db.Users.Find(httpContext.User.Identity.GetUserId()); var spotz = new Spotz { SpotzId = Guid.NewGuid(), Title = vm.Title, Description = vm.Description ?? "", User = user, Timestamp = DateTime.Now, Latitude = vm.Latitude ?? "", Longitude = vm.Longitude ?? "" }; var listOfTags = new List <Tag>(); // Lager liste med tags fra kommaseparert string som skal legges i Spotz if (vm.TagNames != null) { var listOfTagNames = vm.TagNames.TrimEnd(',').Split(',').ToList(); foreach (var tag in listOfTagNames) { var currentTag = _db.Tags.FirstOrDefault(t => t.TagName == tag); // Hvis tag IKKE fins fra før: if (currentTag == null) { currentTag = new Tag { TagName = tag, TagId = Guid.NewGuid() }; listOfTags.Add(currentTag); currentTag.Spotzes.Add(spotz); _db.Tags.Add(currentTag); _db.Entry(currentTag).State = EntityState.Added; } else { listOfTags.Add(currentTag); currentTag.Spotzes.Add(spotz); _db.Entry(currentTag).State = EntityState.Modified; } } } spotz.Tags = listOfTags; var request = httpContext.Request; var file = request.Files[0]; var title = request.Form["title"]; var imageId = Guid.NewGuid(); try { var newImage = new Image { Data = _helperMethods.ConvertToByteArray(file.InputStream), Timestamp = DateTime.Now, Filename = title, ImageId = imageId, ImageUrl = string.Format($"/Images/{imageId}"), Spotz = spotz }; spotz.Images.Add(newImage); _db.Spotzes.Add(spotz); _db.Images.Add(newImage); _db.Entry(spotz).State = EntityState.Added; _db.Entry(newImage).State = EntityState.Added; _db.SaveChanges(); return(spotz); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }