public ActionResult Create([Bind(Include = "ID,FirstName,LastName,Country")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
        public ActionResult Create([Bind(Include = "ID,AuthorID,Content,Source,Category")] Quote quote)
        {
            if (ModelState.IsValid)
            {
                db.Quotes.Add(quote);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AuthorID = new SelectList(db.Authors, "ID", "FirstName", quote.AuthorID);
            return(View(quote));
        }
        public IActionResult GetAQuoteInSQL(QuoteViewModel model)
        {
            var watch = Stopwatch.StartNew();

            var Quote = new Quote()
            {
                FirstName = model.FirstName,
                LastName  = model.LastName,
                Email     = model.Email,
                PostCode  = model.PostCode,
                Created   = DateTime.Now
            };

            string quoteNumber = RandomString(8);

            Quote.QuoteNumber = quoteNumber;
            _context.Quotes.Add(Quote);
            _context.SaveChanges();
            var firstCompany = (from c in _context.Quotes select c).FirstOrDefault();
            var kvp          = firstCompany.Id;

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            if (ModelState.IsValid)
            {
                ViewBag.Message = "Quote number is: " + quoteNumber + " Time taken: " + elapsedMs + "ms";
            }
            return(View());
        }
Beispiel #4
0
        public ActionResult Register(RegistrationVM user)
        {
            if (ModelState.IsValid)
            {
                using (var context = new QuoteContext())
                {
                    //check database for existing username
                    if (context.ClientLogins.Any(x => x.Username == user.Username))
                    {
                        ModelState.AddModelError(string.Empty, "Username already exists");
                        return(View(user));
                    }
                    else
                    {
                        var newLogin = new ClientLogin();
                        newLogin.Username = user.Username;
                        newLogin.Password = user.Password;

                        var newClient = new Client();
                        newClient.ClientLogin = newLogin;


                        context.Clients.Add(newClient);
                        context.ClientLogins.Add(newLogin);
                        context.SaveChanges();

                        return(Redirect("/Account/Login"));
                    }
                }
            }

            //Redirect to Login Page if model state valid
            return(View());
        }
        public ActionResult Create(Quote quote)
        {
            quoteDb.Quotes.Add(quote);
            quoteDb.SaveChanges();
            //Console.WriteLine("here");

            return(RedirectToAction("Index"));
        }
        public ActionResult Create(Quote quote)
        {
            ViewBag.error = "";
            if (quote.body == null || quote.authorName == null)
            {
                ViewBag.error = "Please fill all fields on the form";
            }

            try{
                quoteDb.Quotes.Add(quote);
                quoteDb.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch {
                return(View());
            }
        }
Beispiel #7
0
        /// <summary>
        /// Creating database table
        /// </summary>
        /// <param name="context"></param>

        private static void PopulateDatabase(QuoteContext context)
        {
            var author1 = new Author()
            {
                name = "Danny Pham"
            };
            var author2 = new Author()
            {
                name = "Turtle Kid"
            };
            var author3 = new Author()
            {
                name = "Robert Frost"
            };
            var author4 = new Author()
            {
                name = "Marylin Monroe"
            };

            var quote1 = new Quote()
            {
                quote = "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best."
            };
            var quote2 = new Quote()
            {
                quote = "What Am I doing?"
            };
            var quote3 = new Quote()
            {
                quote = "I like Turtles."
            };
            var quote4 = new Quote()
            {
                quote = "In three words I can sum up everything I've learned about life: it goes on."
            };

            var qa1 = new QuoteAuthor()
            {
                Quote = quote1, Author = author4
            };
            var qa2 = new QuoteAuthor()
            {
                Quote = quote2, Author = author1
            };
            var qa3 = new QuoteAuthor()
            {
                Quote = quote3, Author = author2
            };
            var qa4 = new QuoteAuthor()
            {
                Quote = quote4, Author = author3
            };

            context.AddRange(qa1, qa2, qa3, qa4);

            context.SaveChanges();
        }
Beispiel #8
0
 public IActionResult Process(Quote model)
 {
     if (ModelState.IsValid)
     {
         // _quoteFactory.Add(model);
         _context.Quotes.Add(model);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.Errors = ModelState.Values;
     return(View("Landing"));
 }
Beispiel #9
0
        public IActionResult Save([FromBody] QuoteViewModel quote)
        {
            var user = _userManagement.Find(User.FindFirstValue(ClaimTypes.NameIdentifier));

            var   repo = new QuoteRepository(_context);
            Quote q;

            // New Quote
            if (quote.Id == 0)
            {
                q = repo.CreateQuote(user);
            }
            // Update existing
            else
            {
                q = repo.Search(quote.Id);
                if (q == null)
                {
                    // return 404?
                    return(NotFound());
                }
                else
                {
                    if (q.CreatedByUserId != user.Id)
                    {
                        // Not created by this user??
                        return(Unauthorized());
                    }
                }
            }

            q.Text = quote.Text;

            // Check if Author has changed
            if (q.Author == null || q.Author.Name != quote.Author)
            {
                q.Author = repo.FindAuthor(quote.Author);
                if (q.Author == null)
                {
                    q.Author = new Author()
                    {
                        Name = quote.Author,
                    };
                }
            }

            _context.SaveChanges();

            return(Ok(quote));
        }
        public ActionResult Edit(ProfileVM model)
        {
            if (ModelState.IsValid)
            {
                var context = new QuoteContext();
                var login   = context.ClientLogins.Single(x => x.Username == ApplicationSession.Username);
                var client  = context.Clients.Single(x => x.LoginId == login.Id);

                client.Name         = model.Name;
                client.AddressLine1 = model.AddressLine1;
                client.AddressLine2 = model.AddressLine2;
                client.City         = model.City;
                client.State        = model.StateOption.ToString();
                client.ZipCode      = model.Zipcode;

                context.SaveChanges();

                return(Redirect("/Home/UserHub"));
            }
            else
            {
                return(View(model));
            }
        }
Beispiel #11
0
        public async Task AddNewQuote()
        {
            //arrange
            var options = new DbContextOptionsBuilder <QuoteContext>().UseInMemoryDatabase(databaseName: "QuoteTestDb").Options;

            using (var dbContext = new QuoteContext(options))
            {
                var quoteRepository = new QuoteRepository(dbContext);

                //act
                dbContext.Quotes.Add(new Quote()
                {
                    Content = "It’s a dangerous business, Frodo, going out your door. You step onto the road, and if you don’t keep your feet, there’s no knowing where you might be swept off to.",
                    Author  = "Bilbo"
                });
                dbContext.SaveChanges();

                //assert
                string expected = "Bilbo";
                var    quote    = await dbContext.Quotes.FirstOrDefaultAsync(q => q.Author.Equals("Bilbo"));

                Assert.Equal(expected, quote.Author);
            }
        }
Beispiel #12
0
 public IActionResult Register(RegisterViewModel newUser)
 {
     if (ModelState.IsValid)
     {
         user userQuery = _context.Users.SingleOrDefault(user => user.Email == newUser.Email);
         if (userQuery != null)
         {
             ViewBag.Exists = "User already exists.";
             return(View("Index"));
         }
         user addUser = new user {
             FirstName = newUser.FirstName,
             LastName  = newUser.LastName,
             Email     = newUser.Email,
             Password  = newUser.Password,
             // CreatedAt = DateTime.Now,
             // UpdatedAt = DateTime.Now
         };
         _context.Users.Add(addUser);
         _context.SaveChanges();
         return(RedirectToAction("Dashboard", "Dashboard"));
     }
     return(View("Index", newUser));
 }
Beispiel #13
0
 public void AddQuote([FromBody] Quote quote)
 {
     _context.Quotes.Add(quote);
     _context.SaveChanges();
 }
Beispiel #14
0
 public void Complete()
 {
     context.SaveChanges();
 }