Example #1
0
        public ActionResult AdminFeed()
        {
            string          queryString = @"SELECT FeedName, FeedDate, FeedTime, Food from Feed";
            List <Feedings> feedings    = new List <Feedings>();


            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var feeding = new Feedings();
                    feeding.FeedName = reader["FeedName"].ToString();
                    feeding.FeedDate = Convert.ToDateTime(reader["FeedDate"]);
                    feeding.FeedTime = reader["FeedTime"].ToString();
                    feeding.Food     = reader["Food"].ToString();
                    feedings.Add(feeding);
                }
            }
            return(View("AdminFeed"));
        }
Example #2
0
        public ActionResult Create([Bind(Include = "AnimalGroupId,EmployeeId,FoodProductsId,TimeForFeeding,FeedingDate,Quantity")] Feedings feedings)
        {
            ViewBag.Exception = null;
            string msg = null;
            FeedingReminderAccess feedingReminderAccess = new FeedingReminderAccess();

            if (ModelState.IsValid)
            {
                db.Feedings.Add(feedings);
                try
                {
                    FoodProducts foodProducts = db.FoodProducts.Single(c => c.FoodProductsId == feedings.FoodProductsId);
                    foodProducts.Quantity = foodProducts.Quantity - feedings.Quantity ?? default(int);
                    feedingReminderAccess.Create(new FeedingReminder(feedings.FeedingId, feedings.FeedingDate.ToString(), 0));
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    msg = "Unable to create such feeding, we have no more such food products";

                    ViewBag.Exception      = msg;
                    ViewBag.AnimalGroupId  = new SelectList(db.AnimalGroups, "AnimalGroupId", "Name");
                    ViewBag.EmployeeId     = new SelectList(db.Employees, "EmployeeId", "FirstName");
                    ViewBag.FoodProductsId = new SelectList(db.FoodProducts, "FoodProductsId", "Name");

                    return(View(feedings));
                }
                return(RedirectToAction("Index"));
            }

            ViewBag.AnimalGroupId  = new SelectList(db.AnimalGroups, "AnimalGroupId", "Name", feedings.AnimalGroupId);
            ViewBag.EmployeeId     = new SelectList(db.Employees, "EmployeeId", "FirstName", feedings.EmployeeId);
            ViewBag.FoodProductsId = new SelectList(db.FoodProducts, "FoodProductsId", "Name", feedings.FoodProductsId);
            return(View(feedings));
        }
Example #3
0
        public async Task ExecuteLoadFeedingsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                Feedings.Clear();
                var feedings = await _database.GetFeedingsAsync(Item.ID);

                Feedings.ReplaceRange(feedings);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                MessagingCenter.Send(new MessagingCenterAlert
                {
                    Title   = "Error",
                    Message = "Unable to load feedings.",
                    Cancel  = "OK"
                }, "message");
            }
            finally
            {
                IsBusy = false;
            }
        }
Example #4
0
        public ActionResult DeleteConfirmed(int id)
        {
            Feedings feedings = db.Feedings.Find(id);

            db.Feedings.Remove(feedings);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Example #5
0
        public async Task <ActionResult <Feedings> > PostFeedings(Feedings feedings)
        {
            // Indicate to the database context we want to add this new record
            _context.Feeding.Add(feedings);
            await _context.SaveChangesAsync();

            // Return a response that indicates the object was created (status code `201`) and some additional
            // headers with details of the newly created object.
            return(CreatedAtAction("GetFeedings", new { id = feedings.Id }, feedings));
        }
Example #6
0
        // GET: Feedings/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Feedings feedings = db.Feedings.Find(id);

            if (feedings == null)
            {
                return(HttpNotFound());
            }
            return(View(feedings));
        }
Example #7
0
        public ActionResult Edit([Bind(Include = "FeedingId,AnimalGroupId,EmployeeId,FoodProductsId,TimeForFeeding,FeedingDate,RowVersion")] Feedings feedings)
        {
            ViewBag.Exception = null;
            string msg = null;

            if (ModelState.IsValid)
            {
                var entity = db.Feedings.Single(p => p.FeedingId == feedings.FeedingId);

                if (entity.RowVersion != feedings.RowVersion)
                {
                    TempData["Exception"] = "Entity was modified by another user. Check values and perform edit action again";
                    return(RedirectToAction("Edit"));
                }
                entity.RowVersion++;
                entity.AnimalGroupId  = feedings.AnimalGroupId;
                entity.EmployeeId     = feedings.EmployeeId;
                entity.FoodProducts   = feedings.FoodProducts;
                entity.TimeForFeeding = feedings.TimeForFeeding;
                entity.FeedingDate    = feedings.FeedingDate;

                db.Entry(entity).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    if (e.InnerException == null)
                    {
                        msg = e.Message;
                    }
                    else
                    {
                        msg = e.InnerException.InnerException.Message;
                    }

                    ViewBag.Exception = msg;

                    return(View(feedings));
                }
                return(RedirectToAction("Index"));
            }
            ViewBag.AnimalGroupId  = new SelectList(db.AnimalGroups, "AnimalGroupId", "Name", feedings.AnimalGroupId);
            ViewBag.EmployeeId     = new SelectList(db.Employees, "EmployeeId", "FirstName", feedings.EmployeeId);
            ViewBag.FoodProductsId = new SelectList(db.FoodProducts, "FoodProductsId", "Name", feedings.FoodProductsId);
            return(View(feedings));
        }
Example #8
0
        // GET: Feedings/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Feedings feedings = db.Feedings.Find(id);

            if (feedings == null)
            {
                return(HttpNotFound());
            }
            ViewBag.AnimalGroupId  = new SelectList(db.AnimalGroups, "AnimalGroupId", "Name", feedings.AnimalGroupId);
            ViewBag.EmployeeId     = new SelectList(db.Employees, "EmployeeId", "FirstName", feedings.EmployeeId);
            ViewBag.FoodProductsId = new SelectList(db.FoodProducts, "FoodProductsId", "Name", feedings.FoodProductsId);
            return(View(feedings));
        }
Example #9
0
        public async Task <IActionResult> PutFeedings(int id, Feedings feedings)
        {
            // If the ID in the URL does not match the ID in the supplied request body, return a bad request
            if (id != feedings.Id)
            {
                return(BadRequest());
            }

            // Tell the database to consider everything in feedings to be _updated_ values. When
            // the save happens the database will _replace_ the values in the database with the ones from feedings
            _context.Entry(feedings).State = EntityState.Modified;

            try
            {
                // Try to save these changes.
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                // Ooops, looks like there was an error, so check to see if the record we were
                // updating no longer exists.
                if (!FeedingsExists(id))
                {
                    // If the record we tried to update was already deleted by someone else,
                    // return a `404` not found
                    return(NotFound());
                }
                else
                {
                    // Otherwise throw the error back, which will cause the request to fail
                    // and generate an error to the client.
                    throw;
                }
            }

            // return NoContent to indicate the update was done. Alternatively you can use the
            // following to send back a copy of the updated data.
            //
            // return Ok(feedings)
            //
            return(NoContent());
        }
Example #10
0
        public async Task <ActionResult <Feedings> > Feed(int id, Feedings feeding)
        {
            var pet = await _context.Pets.FindAsync(id);

            if (pet == null)
            {
                return(NotFound());
            }
            pet.HappinessLevel += 3;
            pet.HungerLevel    -= 5;
            if (pet.HungerLevel < 0)
            {
                pet.HungerLevel = 0;
            }
            feeding.When  = DateTime.Now;
            feeding.PetId = pet.Id;
            _context.Feedings.Add(feeding);
            await _context.SaveChangesAsync();

            return(Ok(feeding));
        }