Beispiel #1
0
        public async Task <IActionResult> PutCharacterFridge(int id, CharacterFridge characterFridge)
        {
            if (id != characterFridge.CharacterFridgeId)
            {
                return(BadRequest());
            }

            _context.Entry(characterFridge).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CharacterFridgeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.ProductId)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #3
0
        public async Task <IActionResult> PutFridge(Fridge fridge)
        {
            SqlParameter param1     = new SqlParameter("@FoodName", fridge.FoodName);
            Fridge       fridgeData = _context.Fridge.FromSqlRaw("Select * from fridge where foodname = @FoodName", param1).FirstOrDefault();
            SqlParameter param2     = new SqlParameter("@Quantity", (fridgeData.Quantity + 1));

            _context.Database.ExecuteSqlRaw("Update fridge set quantity=@Quantity where foodname = @FoodName", param2, param1);

            _context.Entry(fridge).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FridgeExists(fridge.FridgeId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
 public async Task<IActionResult> Create([Bind("CharacterId,Name,Hunger,Sleep,Awake,Fun,Currency,UserId")] Character character)
 {
     if (ModelState.IsValid)
     {
         _context.Add(character);
         await _context.SaveChangesAsync();
         return RedirectToAction(nameof(Index));
     }
     ViewData["UserId"] = new SelectList(_context.User, "UserId", "UserId", character.UserId);
     return View(character);
 }
Beispiel #5
0
        public async Task <IActionResult> Create([Bind("UserId,Name,Email,Password")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
Beispiel #6
0
        public async Task <IActionResult> Signup([Bind("user,character")] Combiner combiner)
        {
            //Variable that checks whether the email provided already exists in the database
            //This is done by using a lambda function that goes through each user email in the database
            //and compares it with the given one.

            bool result = _context.User.Where(x => x.Email == combiner.user.Email).Any();

            if (ModelState.IsValid && !result)
            {
                //Local Variables that serve as a way to assign specific values to the raw sql query after them
                //Each one represents a different given value
                SqlParameter param1 = new SqlParameter("@UserEmail", combiner.user.Email);
                SqlParameter param2 = new SqlParameter("@UserPassword", combiner.user.Password);
                SqlParameter param3 = new SqlParameter("@CharacterName", combiner.character.Name);

                //A dbcontext function that is cable of executing raw sql code in mvc
                //Here it executes a stored procedure using the variables from before
                _context.Database.ExecuteSqlRaw("createNewUser @UserEmail, @UserPassword, @CharacterName", param1, param2, param3);
                await _context.SaveChangesAsync();

                return(View("Index"));
            }
            else if (result)
            {
                ModelState.AddModelError("user.Email", "Login already exists");
            }
            return(View("Singup", combiner));
        }
        public async Task <IActionResult> Create(FileDetail detail)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    List <BlobSummary> fileDetails = new List <BlobSummary>();
                    for (int i = 0; i < Request.Form.Files.Count; i++)
                    {
                        var file = Request.Form.Files[i];

                        if (file != null)
                        {
                            var fileName = Path.GetFileName(file.FileName);

                            BlobSummary fileDetail = new BlobSummary()
                            {
                                FileName    = fileName,
                                Extension   = Path.GetExtension(fileName),
                                Id          = Guid.NewGuid(),
                                Blob        = GenerateBlob(file),
                                ContentType = Request.Form.Files[i].ContentType
                            };
                            fileDetails.Add(fileDetail);
                        }
                    }

                    detail.Modified = DateTime.Now;
                    detail.Blobs    = fileDetails;

                    _context.FileDetail.Add(detail);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(detail));
        }
        private async Task <ActionResult> TryContextSaveAsync()
        {
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {
                _logger.LogError("DbUpdateConcurrencyException", e);
                return(Problem("Could not save to Database", statusCode: 500, title: "Persistence Error"));
            }
            catch (Exception exp)
            {
                _logger.LogError("Exception", exp);
                return(Problem("Could not save to Database", statusCode: 500, title: "Error"));
            }

            return(null);
        }