Beispiel #1
0
        // GET: Posilek
        public async Task <IActionResult> Index(String searchString, int sort)
        {
            ViewData["currentSearchString"] = searchString;
            ViewData["currentSort"]         = (sort + 1) % 3;

            ViewBag.userId = int.Parse(User.Identity.GetUserId());

            Rola usersRole = _context.role.Include(k => k.uzytkownicy)
                             .FirstOrDefault(m => m.nazwa == "dietetyk");

            List <int> dieticiansIds = new List <int>();

            if (usersRole != null)
            {
                foreach (var user in usersRole.uzytkownicy)
                {
                    dieticiansIds.Add(user.id_uzytkownika);
                }
            }

            ViewBag.dieticiansIds = dieticiansIds;

            var meals = _context.posilki.Where(k => true);

            if (!String.IsNullOrEmpty(searchString))
            {
                meals = meals.Where(k => k.nazwa.Contains(searchString));
            }
            switch (sort)
            {
            case 2:
                ViewBag.ratingsCounted = _context.ocenyPosilkow.GroupBy(t => t.id_posilku)
                                         .Select(t => new { posilek = t.Key, Avg = t.Average(k => k.ocena) }).OrderBy(t => t.Avg)
                                         .AsEnumerable()
                                         .ToDictionary(k => k.posilek, v => v.Avg);
                break;

            case 1:
                ViewBag.ratingsCounted = _context.ocenyPosilkow.GroupBy(t => t.id_posilku)
                                         .Select(t => new { posilek = t.Key, Avg = t.Average(k => k.ocena) }).OrderByDescending(t => t.Avg)
                                         .AsEnumerable()
                                         .ToDictionary(k => k.posilek, v => v.Avg);
                break;

            default:
                ViewBag.ratingsCounted = _context.ocenyPosilkow.GroupBy(t => t.id_posilku)
                                         .Select(t => new { posilek = t.Key, Avg = t.Average(k => k.ocena) })
                                         .AsEnumerable()
                                         .ToDictionary(k => k.posilek, v => v.Avg);
                break;
            }

            /* ViewBag.ratingsCounted = _context.ocenyPosilkow.GroupBy(t => t.id_posilku)
             *                                               .Select(t => new { posilek = t.Key, Avg = t.Average(k => k.ocena) })
             *                                               .AsEnumerable()
             *                                               .ToDictionary(k => k.posilek, v => v.Avg);*/

            this.isDietician();
            return(View(await meals.Include(t => t.uzytkownik).Include(k => k.obrazy).ToListAsync()));
        }
Beispiel #2
0
        // GET: KategoriaTreningu/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var category = await _context.kategoriaTreningu.Include(k => k.treningi)
                           .FirstOrDefaultAsync(m => m.id_kategorii == id);

            ViewBag.trainings = category.treningi;
            ViewBag.userId    = int.Parse(User.Identity.GetUserId());

            Rola usersRole = _context.role.Include(k => k.uzytkownicy)
                             .FirstOrDefault(m => m.nazwa == "trener");

            List <int> trainersIds = new List <int>();

            if (usersRole != null)
            {
                foreach (var user in usersRole.uzytkownicy)
                {
                    trainersIds.Add(user.id_uzytkownika);
                }
            }

            ViewBag.trainersIds = trainersIds;

            if (category == null)
            {
                return(NotFound());
            }
            this.isTrainer();
            return(View(category));
        }
Beispiel #3
0
        // returns -1 when user isn't a trainer
        // returns -2 when user doesn't exist
        private double trainersRating(int user)
        {
            if (!userExists(user))
            {
                return(-2);
            }
            if (!isTrainer(user))
            {
                return(-1);
            }

            Rola role = _context.role.FirstOrDefault(k => k.nazwa == "trener");

            /*double ratings_sum = _context.oceny
             *                        .Where(k => k.id_uzytkownika_ocenianego == user && k.id_roli == role.id_roli)
             *                        .Sum(k => k.ocena);
             * double ratings_count = _context.oceny
             *                        .Where(k => k.id_uzytkownika_ocenianego == user && k.id_roli == role.id_roli)
             *                        .Count();
             * return ratings_sum / (double)ratings_count;*/
            if (!_context.oceny.Any(k => k.id_uzytkownika_ocenianego == user && k.id_roli == role.id_roli))
            {
                return(0);
            }
            double ratings_avg = _context.oceny
                                 .Where(k => k.id_uzytkownika_ocenianego == user && k.id_roli == role.id_roli)
                                 .Average(k => k.ocena);

            return(ratings_avg);
        }
Beispiel #4
0
        public async Task <IActionResult> Edit(int id, [Bind("id_roli,nazwa")] Rola rola)
        {
            if (id != rola.id_roli)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(rola);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RolaExists(rola.id_roli))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(rola));
        }
Beispiel #5
0
        private bool isDietician(int user)
        {
            if (!_context.role.Any(k => k.nazwa == "dietetyk"))
            {
                return(false);
            }
            Rola role = _context.role.FirstOrDefault(k => k.nazwa == "dietetyk");

            return(_context.RolaUzytkownika.Any(k => k.id_uzytkownika == user && k.id_roli == role.id_roli));
        }
Beispiel #6
0
        private bool isTrainer(int user)
        {
            if (!_context.role.Any(k => k.nazwa == "trener"))
            {
                return(false);
            }
            Rola role = _context.role.FirstOrDefault(k => k.nazwa == "trener");

            return(_context.RolaUzytkownika.Any(k => k.id_uzytkownika == user && k.id_roli == role.id_roli));
        }
Beispiel #7
0
        public async Task <IActionResult> Create([Bind("id_roli,nazwa")] Rola rola)
        {
            if (ModelState.IsValid)
            {
                _context.Add(rola);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(rola));
        }
Beispiel #8
0
        private void getListOfRatingsDieticians()
        {
            Dictionary <Uzytkownik, double> listOfRatings = new Dictionary <Uzytkownik, double>();
            Rola role        = _context.role.FirstOrDefault(k => k.nazwa == "dietetyk");
            var  listOfUsers = _context.RolaUzytkownika.Include(k => k.uzytkownik).Where(k => k.id_roli == role.id_roli);

            foreach (var usersRole in listOfUsers)
            {
                listOfRatings.Add(usersRole.uzytkownik,
                                  dieticianRating(usersRole.id_uzytkownika));
            }
        }
Beispiel #9
0
        // GET: Trening
        public async Task <IActionResult> Index(String searchString, String category)
        {
            ViewData["currentSearchString"] = searchString;

            ViewBag.userId = int.Parse(User.Identity.GetUserId());

            Rola usersRole = _context.role.Include(k => k.uzytkownicy)
                             .FirstOrDefault(m => m.nazwa == "trener");

            List <int> trainersIds = new List <int>();

            if (usersRole != null)
            {
                foreach (var user in usersRole.uzytkownicy)
                {
                    trainersIds.Add(user.id_uzytkownika);
                }
            }

            ViewBag.trainersIds = trainersIds;

            SelectList            categories  = new SelectList(_context.kategoriaTreningu, "id_kategorii", "nazwa");
            List <SelectListItem> _categories = categories.ToList();

            _categories.Insert(0, new SelectListItem()
            {
                Value = "-1", Text = "Wszystkie"
            });
            ViewBag.category = new SelectList((IEnumerable <SelectListItem>)_categories, "Value", "Text");

            var trainings = _context.treningi.Where(k => true);

            if (!String.IsNullOrEmpty(searchString))
            {
                trainings = trainings.Where(k => k.nazwa.Contains(searchString));
            }

            if (!String.IsNullOrEmpty(category))
            {
                int category_id = int.Parse(category);
                if (category_id != -1)
                {
                    trainings = trainings.Where(k => k.id_kategorii == category_id);
                }
            }

            return(View(await trainings.Include(t => t.kategoria).Include(t => t.uzytkownik).ToListAsync()));
        }
Beispiel #10
0
        public async Task <IActionResult> Create([Bind("id_roli,nazwa")] Rola rola)
        {
            if (!isAdmin())
            {
                ViewBag.roleName = "admin";
                return(View("UnableToAccessThisPage"));
            }

            if (ModelState.IsValid)
            {
                _context.Add(rola);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(rola));
        }
Beispiel #11
0
        // returns -2 when user doesn't exist
        // return -1 when user isn't a dietician
        private double dieticianRating(int user)
        {
            if (!userExists(user))
            {
                return(-2);
            }
            if (!isDietician(user))
            {
                return(-1);
            }

            Rola role = _context.role.FirstOrDefault(k => k.nazwa == "dietetyk");

            if (!_context.oceny.Any(k => k.id_uzytkownika_ocenianego == user && k.id_roli == role.id_roli))
            {
                return(0);
            }
            double ratings_avg = _context.oceny
                                 .Where(k => k.id_uzytkownika_ocenianego == user && k.id_roli == role.id_roli)
                                 .Average(k => k.ocena);

            return(ratings_avg);
        }
        // GET: Trening
        public async Task <IActionResult> Index(String searchString, String category, int sort)
        {
            ViewData["currentSearchString"] = searchString;
            ViewData["currentSort"]         = (sort + 1) % 3;

            ViewBag.userId = int.Parse(User.Identity.GetUserId());

            Rola usersRole = _context.role.Include(k => k.uzytkownicy)
                             .FirstOrDefault(m => m.nazwa == "trener");

            List <int> trainersIds = new List <int>();

            if (usersRole != null)
            {
                foreach (var user in usersRole.uzytkownicy)
                {
                    trainersIds.Add(user.id_uzytkownika);
                }
            }

            ViewBag.trainersIds = trainersIds;

            SelectList            categories  = new SelectList(_context.kategoriaTreningu, "id_kategorii", "nazwa");
            List <SelectListItem> _categories = categories.ToList();

            _categories.Insert(0, new SelectListItem()
            {
                Value = "-1", Text = "Wszystkie"
            });
            ViewBag.category = new SelectList((IEnumerable <SelectListItem>)_categories, "Value", "Text");

            var trainings = _context.treningi.Where(k => true);

            if (!String.IsNullOrEmpty(searchString))
            {
                trainings = trainings.Where(k => k.nazwa.Contains(searchString));
            }

            if (!String.IsNullOrEmpty(category))
            {
                int category_id = int.Parse(category);
                if (category_id != -1)
                {
                    trainings = trainings.Where(k => k.id_kategorii == category_id);
                }
            }

            switch (sort)
            {
            case 2:
                ViewBag.ratingsCounted = _context.ocenyTreningow.GroupBy(t => t.id_treningu)
                                         .Select(t => new { training = t.Key, Avg = t.Average(k => k.ocena) }).OrderBy(t => t.Avg)
                                         .AsEnumerable()
                                         .ToDictionary(k => k.training, v => v.Avg);
                break;

            case 1:
                ViewBag.ratingsCounted = _context.ocenyTreningow.GroupBy(t => t.id_treningu)
                                         .Select(t => new { training = t.Key, Avg = t.Average(k => k.ocena) }).OrderByDescending(t => t.Avg)
                                         .AsEnumerable()
                                         .ToDictionary(k => k.training, v => v.Avg);
                break;

            default:
                ViewBag.ratingsCounted = _context.ocenyTreningow.GroupBy(t => t.id_treningu)
                                         .Select(t => new { training = t.Key, Avg = t.Average(k => k.ocena) })
                                         .AsEnumerable()
                                         .ToDictionary(k => k.training, v => v.Avg);
                break;
            }

            /*ViewBag.ratingsCounted = _context.ocenyTreningow.GroupBy(t => t.id_treningu)
             *                                              .Select(t => new { training = t.Key, Avg = t.Average(k => k.ocena) })
             *                                              .AsEnumerable()
             *                                              .ToDictionary(k => k.training, v => v.Avg);*/
            isTrainer();
            return(View(await trainings.Include(t => t.kategoria).Include(t => t.obrazy).Include(t => t.uzytkownik).ToListAsync()));
        }
        // GET: Trening/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            ViewBag.Message = "";
            if (id == null)
            {
                return(NotFound());
            }

            var trening = await _context.treningi
                          .Include(t => t.kategoria)
                          .Include(t => t.uzytkownik)
                          .Include(t => t.obrazy)
                          .FirstOrDefaultAsync(m => m.id_treningu == id);

            if (trening == null)
            {
                return(NotFound());
            }

            ViewBag.trainingDetails = _context.treningSzczegoly.Where(k => k.id_treningu == id)
                                      .Include(k => k.cwiczenie)
                                      .ToList();
            var  userId    = int.Parse(User.Identity.GetUserId());
            Rola usersRole = _context.role.Include(k => k.uzytkownicy)
                             .FirstOrDefault(m => m.nazwa == "trener");

            List <int> trainersIds = new List <int>();

            if (usersRole != null)
            {
                foreach (var user in usersRole.uzytkownicy)
                {
                    trainersIds.Add(user.id_uzytkownika);
                }
            }
            if (userId != trening.id_uzytkownika && !trainersIds.Contains(trening.id_uzytkownika))
            {
                return(NotFound());
            }

            try
            {
                ViewBag.rating = _context.ocenyTreningow.Single(e => e.id_uzytkownika == userId && e.id_treningu == id);
            }
            catch
            {
                var rate = new OcenaTreningu();
                rate.ocena     = 0;
                ViewBag.rating = rate;
            }

            ViewBag.trainingId   = id;
            ViewBag.userId       = int.Parse(this.User.Identity.GetUserId());
            ViewBag.treningOwner = trening.id_uzytkownika;

            if (trening == null)
            {
                return(NotFound());
            }

            if (trening.obrazy.Count <= 0)
            {
                ViewBag.image = null;
            }
            else
            {
                ViewBag.image = trening.obrazy
                                .Last()
                                .GetImageDataUrl();
            }

            var link = generateYoutubeEmbededLink(trening.youtube_link);

            ViewBag.youtube = link == "" || link == null ? null : link;
            isTrainer();
            return(View(trening));
        }
Beispiel #14
0
        // GET: Posilek/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            ViewBag.id = id;

            var posilek = await _context.posilki
                          .Include(p => p.uzytkownik)
                          .Include(p => p.obrazy)
                          .FirstOrDefaultAsync(m => m.id_posilku == id);

            if (posilek == null)
            {
                return(NotFound());
            }

            int  userId    = int.Parse(this.User.Identity.GetUserId());
            Rola usersRole = _context.role.Include(k => k.uzytkownicy)
                             .FirstOrDefault(m => m.nazwa == "dietetyk");
            List <int> dieticiansIds = new List <int>();

            if (usersRole != null)
            {
                foreach (var user in usersRole.uzytkownicy)
                {
                    dieticiansIds.Add(user.id_uzytkownika);
                }
            }
            if (userId != posilek.id_uzytkownika && !dieticiansIds.Contains(posilek.id_uzytkownika))
            {
                return(NotFound());
            }
            ViewBag.userId = userId;

            try
            {
                ViewBag.ocena = _context.ocenyPosilkow.Single(e => e.id_uzytkownika == userId && e.id_posilku == posilek.id_posilku);
            }
            catch
            {
                var rate = new OcenaPosilku();
                rate.ocena    = 0;
                ViewBag.ocena = rate;
            }
            ViewBag.srednia = avgRating(posilek.id_posilku);

            ViewBag.mealsDetails = _context.posilekSzczegoly.Where(k => k.id_posilku == id)
                                   .Include(k => k.skladnik)
                                   .ToList();

            ViewBag.posilekOwner = posilek.id_uzytkownika;

            if (posilek == null)
            {
                return(NotFound());
            }

            if (posilek.obrazy.Count <= 0)
            {
                ViewBag.image = null;
            }
            else
            {
                ViewBag.image = posilek.obrazy
                                .Last()
                                .GetImageDataUrl();
            }

            this.isDietician();
            return(View(posilek));
        }