コード例 #1
0
        public IActionResult Add(Activity Act)
        {
            if (ModelState.IsValid == false)
            {
                ViewBag.Errors = ModelState.Values;
                ViewBag.Status = true;
                return(View("AddPage"));
            }
            else
            {
                if (Act.date < DateTime.Now.Date)
                {
                    TempData["message"] = "Date cant be in the past";
                    return(RedirectToAction("AddPage"));
                }
                else
                {
                    Act.CreatedAt = DateTime.Now;
                    Act.UpdatedAt = DateTime.Now;
                    Act.UserId    = (int)HttpContext.Session.GetInt32("UserId");
                    _context.Add(Act);
                    _context.SaveChanges();
                    TempData["message"] = "Sucessfully added a new activity";
                }
            }
            Activity Activity = new Activity();

            Activity = _context.activities.Where(activity => activity.CreatedAt.Ticks == Act.CreatedAt.Ticks).SingleOrDefault();
            int ID = Activity.id;

            return(RedirectToAction("Page", new { @ActId = ID }));
        }
コード例 #2
0
 public IActionResult Register(RegisterViewModel model)
 {
     if (_context.users.Where(u => u.email == model.email).SingleOrDefault() != null)
     {
         TempData["message"] = "Email is taken!";
         return(RedirectToAction("Index"));
     }
     else
     {
         if (ModelState.IsValid)
         {
             User NewUser = new User {
                 FirstName = model.FirstName,
                 LastName  = model.LastName,
                 email     = model.email,
                 password  = model.password
             };
             NewUser.CreatedAt = DateTime.Now;
             NewUser.UpdatedAt = DateTime.Now;
             _context.Add(NewUser);
             _context.SaveChanges();
             User current = _context.users.Where(u => u.email == model.email).SingleOrDefault();
             HttpContext.Session.SetInt32("UserId", current.id);
             TempData["message"] = "You have successfully registered!";
             return(RedirectToAction("Activity", "Activity"));
         }
         ViewBag.Errors = ModelState.Values;
         ViewBag.Status = true;
         return(View("Index"));
     }
 }
コード例 #3
0
ファイル: OwnersController.cs プロジェクト: ivka125/Dog-Shop
        public async Task <IActionResult> Create([Bind("Id,Name,Age")] Owner owner)
        {
            if (ModelState.IsValid)
            {
                _context.Add(owner);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(owner));
        }
コード例 #4
0
ファイル: DogController.cs プロジェクト: TayRome/FurLove
        public IActionResult SetPreferences(int PrefAge, int PrefBreed, int PrefBodyType, int PrefEducation, int PrefAccidents, int PrefBarking)
        {
            int?dogId      = HttpContext.Session.GetInt32("CurrentDog");
            Dog CurrentDog = _context.dogs.Include(d => d.Preferences).ThenInclude(p => p.Filter).SingleOrDefault(dog => dog.DogId == dogId);
            List <Preference> AllNewPreferences = new List <Preference>();

            if (PrefAge > 0)
            {
                Preference currPrefAge = CurrentDog.Preferences.SingleOrDefault(p => p.Filter.Category == "Age");
                if (currPrefAge != null)
                {
                    _context.Preferences.Remove(currPrefAge);
                }
                Preference newPreference = new Preference();
                newPreference.DogId    = CurrentDog.DogId;
                newPreference.FilterId = PrefAge;
                AllNewPreferences.Add(newPreference);
            }
            if (PrefBreed > 0)
            {
                Preference currPrefBreed = CurrentDog.Preferences.SingleOrDefault(p => p.Filter.Category == "Breed");
                if (currPrefBreed != null)
                {
                    _context.Preferences.Remove(currPrefBreed);
                }
                Preference newPreference = new Preference();
                newPreference.DogId    = CurrentDog.DogId;
                newPreference.FilterId = PrefBreed;
                AllNewPreferences.Add(newPreference);
            }
            if (PrefBodyType > 0)
            {
                Preference currPrefBodyType = CurrentDog.Preferences.SingleOrDefault(p => p.Filter.Category == "BodyType");
                if (currPrefBodyType != null)
                {
                    _context.Preferences.Remove(currPrefBodyType);
                }
                Preference newPreference = new Preference();
                newPreference.DogId    = CurrentDog.DogId;
                newPreference.FilterId = PrefBodyType;
                AllNewPreferences.Add(newPreference);
            }
            if (PrefEducation > 0)
            {
                Preference currPrefEducation = CurrentDog.Preferences.SingleOrDefault(p => p.Filter.Category == "Education");
                if (currPrefEducation != null)
                {
                    _context.Preferences.Remove(currPrefEducation);
                }
                Preference newPreference = new Preference();
                newPreference.DogId    = CurrentDog.DogId;
                newPreference.FilterId = PrefEducation;
                AllNewPreferences.Add(newPreference);
            }
            if (PrefAccidents > 0)
            {
                Preference currPrefAccidents = CurrentDog.Preferences.SingleOrDefault(p => p.Filter.Category == "Accidents");
                if (currPrefAccidents != null)
                {
                    _context.Preferences.Remove(currPrefAccidents);
                }
                Preference newPreference = new Preference();
                newPreference.DogId    = CurrentDog.DogId;
                newPreference.FilterId = PrefAccidents;
                AllNewPreferences.Add(newPreference);
            }
            if (PrefBarking > 0)
            {
                Preference currPrefBarking = CurrentDog.Preferences.SingleOrDefault(p => p.Filter.Category == "Barking");
                if (currPrefBarking != null)
                {
                    _context.Preferences.Remove(currPrefBarking);
                }
                Preference newPreference = new Preference();
                newPreference.DogId    = CurrentDog.DogId;
                newPreference.FilterId = PrefBarking;
                AllNewPreferences.Add(newPreference);
            }
            foreach (Preference preference in AllNewPreferences)
            {
                _context.Add(preference);
            }
            _context.SaveChanges();
            return(RedirectToAction("Profile", new{ DogId = dogId }));
        }