public HttpResponseMessage CreateQuote(QuotationViewModel quote) { if (User.Identity.IsAuthenticated) { var user = userManager.FindById(User.Identity.GetUserId()); using (var categoryCreator = new CategoryCreationHelper()) { categoryCreator.TryCreateCategory(quote.Category); } // Grab the category var category = db.Categories.Where(c => c.Name.Equals(quote.Category)).First(); // Create the quote var newQuote = new Quotation { Author = quote.Author, Category = category, DateAdded = DateTime.Now, Quote = quote.Quote, User = user }; db.Quotations.Add(newQuote); db.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created); } else { return Request.CreateResponse(HttpStatusCode.Forbidden); } }
public HttpResponseMessage UpdateQuote(int id, QuotationViewModel quote) { var quoteToUpdate = db.Quotations.Where(q => q.QuotationId == id).FirstOrDefault(); var user = userManager.FindById(User.Identity.GetUserId()); if (quoteToUpdate == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } else if (User.IsInRole("admin") || (User.Identity.IsAuthenticated && quoteToUpdate.User.Id.Equals(user.Id))) { using (var categoryCreator = new CategoryCreationHelper()) { categoryCreator.TryCreateCategory(quote.Category); } // Grab the category var category = db.Categories.Where(c => c.Name.Equals(quote.Category)).First(); // Update the quotation db.Entry(quoteToUpdate).State = System.Data.Entity.EntityState.Modified; var entity = db.Entry(quoteToUpdate).Entity; entity.Category = category; entity.Author = quote.Author; entity.Quote = quote.Quote; db.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.Forbidden); } }
public ActionResult ImportQuotes(string importUrl) { // Message to send back to the index method string importResult; // Make a request to the URL HttpClient client = new HttpClient(); HttpResponseMessage response; // Wrap this to check for an invalid URL try { response = client.GetAsync(importUrl).Result; } catch (InvalidOperationException e) { importResult = "That URL was invalid. Make sure you use the FULL url (http:// included)."; return RedirectToAction("Index", new { importResult = importResult, imported = false }); } // If we couldn't get the quotes, redirect with an error message if (response.StatusCode != HttpStatusCode.OK) { importResult = "There was a problem importing from that URL."; return RedirectToAction("Index", new { importResult = importResult, imported = false }); } // Else, try to get the quotes IEnumerable<QuotationViewModel> quotes; try { quotes = response.Content.ReadAsAsync<IEnumerable<QuotationViewModel>>().Result; } catch (UnsupportedMediaTypeException e) { importResult = "There was a problem importing from that URL."; return RedirectToAction("Index", new { importResult = importResult, imported = false }); } foreach (var quote in quotes) { // We'll only add a quote if it doesn't already exist in the database if (db.Quotations.Where(q => q.Quote.Equals(quote.Quote)).Count() == 0) { using (var categoryCreator = new CategoryCreationHelper()) { categoryCreator.TryCreateCategory(quote.Category); } // Grab the category var category = db.Categories.Where(c => c.Name.Equals(quote.Category)).First(); // Add the new quotation via the Quotations controller var user = manager.FindById(User.Identity.GetUserId()); db.Quotations.Add(new Quotation { Quote = quote.Quote, Author = quote.Author, CategoryId = category.CategoryId, User = user, DateAdded = DateTime.Today }); db.SaveChanges(); } } importResult = "Quotes imported successfully!"; return RedirectToAction("Index", new { importResult = importResult, imported = true }); }