Beispiel #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            FitnessEvent fitnessEvent = db.FitnessEvents.Find(id);

            db.FitnessEvents.Remove(fitnessEvent);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Beispiel #2
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            FitnessEvent fitnessEvent = db.FitnessEvents.Find(id);

            if (fitnessEvent == null)
            {
                return(HttpNotFound());
            }
            return(View(fitnessEvent));
        }
Beispiel #3
0
        public ActionResult Edit([Bind(Include = "Id,EventName,EventDetails,EventDate,EventType,PostedDate,ApplicationUserId")]
                                 FitnessEvent fitnessEvent, HttpPostedFileBase postedFile)
        {
            if (ModelState.IsValid)
            {
                if (postedFile != null)
                {
                    string serverPath = Server.MapPath("~/Uploads/");
                    // string fileExtension = ImageUrl.GetExtension(postedFile.FileName);
                    string filePath = fitnessEvent.ImageUrl + ".jpg";
                    fitnessEvent.ImageUrl = filePath;
                    postedFile.SaveAs(serverPath + fitnessEvent.ImageUrl);
                }


                db.Entry(fitnessEvent).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(fitnessEvent));
        }
Beispiel #4
0
        public ActionResult Create([Bind(Include = "Id,EventName,EventDetails,EventDate,EventType,PostedDate,ApplicationUserId")]
                                   FitnessEvent fitnessEvent, HttpPostedFileBase postedFile)
        {
            ModelState.Clear();
            var myUniqueFileName = string.Format(@"{0}", Guid.NewGuid());

            fitnessEvent.ImageUrl = myUniqueFileName;
            TryValidateModel(fitnessEvent);

            if (ModelState.IsValid)
            {
                string serverPath = Server.MapPath("~/Uploads/");
                // string fileExtension = ImageUrl.GetExtension(postedFile.FileName);
                string filePath = fitnessEvent.ImageUrl + ".jpg";
                fitnessEvent.ImageUrl = filePath;
                postedFile.SaveAs(serverPath + fitnessEvent.ImageUrl);

                db.FitnessEvents.Add(fitnessEvent);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(fitnessEvent));
        }
Beispiel #5
0
        public async Task <IActionResult> Create([Bind("Title,Description,StartDate,EndDate,EventCategoryId,GoalId,GoalTypeId,EventStatusId")] Event newEvent, int financialAmount, int fitnessDistance, int fitnessDuration)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new ArgumentNullException("User.FindFirstValue(ClaimTypes.NameIdentifier)");

            newEvent.CreatedAt = DateTime.Now;
            newEvent.UserId    = User.FindFirstValue(ClaimTypes.NameIdentifier);


            if (ModelState.IsValid)
            {
                bool goalAdded = false;
                var  goalType  = await _context.GoalTypes.FindAsync(newEvent.GoalTypeId);

                var goalStatuses = await _context.GoalStatuses.ToListAsync();


                if (newEvent.GoalId != 0)
                {
                    goalAdded = true;
                }

                if (goalType.Name == "Financial")
                {
                    var financialEvent = new FinancialEvent {
                        Amount = financialAmount
                    };
                    var wallet = await _context.Wallets.Where(x => x.UserId == userId).ToListAsync();

                    wallet[0].Amount -= financialAmount;
                    _context.Update(wallet[0]);

                    if (goalAdded)
                    {
                        var goal = await _context.Goals.FindAsync(newEvent.GoalId);

                        goal.NumericalProgress += financialAmount;
                        if (goal.NumericalProgress >= goal.NumericalTarget)
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "Succeeded").Id;
                        }
                        else
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "In Progress").Id;
                        }

                        _context.Update(goal);
                    }

                    await _context.FinancialEvents.AddAsync(financialEvent);

                    await _context.SaveChangesAsync();

                    newEvent.FinancialEventId = financialEvent.Id;
                }
                else if (goalType.Name == "Fitness")
                {
                    var fitnessEvent = new FitnessEvent();
                    fitnessEvent.Distance = fitnessDistance;
                    fitnessEvent.Duration = 0;

                    if (goalAdded)
                    {
                        var goal = await _context.Goals.FindAsync(newEvent.GoalId);

                        goal.NumericalProgress += fitnessDistance;
                        if (goal.NumericalProgress >= goal.NumericalTarget)
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "Succeeded").Id;
                        }
                        else
                        {
                            goal.GoalStatusId = goalStatuses.Find(x => x.Status == "In Progress").Id;
                        }

                        _context.Update(goal);
                    }

                    await _context.FitnessEvents.AddAsync(fitnessEvent);

                    await _context.SaveChangesAsync();

                    newEvent.FitnessEventId = fitnessEvent.Id;
                }
                else if (goalType.Name == "General")
                {
                    if (goalAdded)
                    {
                        var goal = await _context.Goals.FindAsync(newEvent.GoalId);

                        goal.GoalStatusId = goalStatuses.Find(x => x.Status == "Succeeded").Id;

                        _context.Update(goal);
                    }
                    await _context.SaveChangesAsync();
                }

                await _context.AddAsync(newEvent);

                await _context.SaveChangesAsync();

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