Example #1
0
 public IActionResult Post([FromBody] Product product)
 {
     if (ModelState.IsValid)
     {
         db.Products.Add(product);
         db.SaveChanges();
         return(Ok(product));
     }
     return(BadRequest(ModelState));
 }
Example #2
0
        public IActionResult Post([FromBody] ViewRelation relation)
        {
            Relation buffer = db.Relations.FirstOrDefault((Relation x) => x.UserName == User.Identity.Name && x.ProductId == relation.ProductId);

            if (buffer != null)
            {
                string   username = User.Identity.Name;
                Relation product  = db.Relations.FirstOrDefault(x => x.ProductId == relation.ProductId && x.UserName == username && x.Checked == true);
                db.Relations.Remove(product);
                db.SaveChanges();
                return(Ok(relation));
            }
            Relation dbrelationmodel = new Relation()
            {
                ProductId = relation.ProductId, Checked = true, UserName = User.Identity.Name
            };

            db.Relations.Add(dbrelationmodel);
            db.SaveChanges();
            return(Ok(relation));
        }
Example #3
0
 public SampleDataController(FullContext context)
 {
     db = context;
     if (!db.Products.Any())
     {
         db.Products.Add(new Product {
             Name = "iPhone X", Designer = "Apple", Cost = 79900, About = "Premium class smartphone"
         });
         db.Products.Add(new Product {
             Name = "Galaxy S8", Designer = "Samsung", Cost = 49900, About = "Flagman Samsung smartphone with better ergonomics abilities"
         });
         db.Products.Add(new Product {
             Name = "Pixel 2", Designer = "Google", Cost = 52900, About = "Most powered smartphone"
         });
         db.SaveChanges();
     }
 }