예제 #1
0
        public ActionResult Create(UserModel user)
        {
            if (ModelState.IsValid)
            {
                if (user.BirthDate > DateTime.Now)
                {
                    ModelState.AddModelError("BirthDate", "Jesteś z przyszłości?");
                    return View(user);
                }

                try
                {
                    var dao = new UserService();

                    var u = new User
                    {
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        BirthDate = user.BirthDate,
                        Email = user.Email,
                        Phone = user.Phone,
                        AddDate = user.AddDate,
                        IsActive = user.IsActive
                    };

                    var err = dao.CreateUser(u);
                    if (err != null)
                    {
                        ModelState.AddModelError("", "Wystąpił problem podczas dodawania nowego użytkownika.");
                        return View(user);
                    }

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    return View(user);
                }
            }
            else
            {
                return View(user);
            }
        }
예제 #2
0
        public string CreateUser(User user)
        {
            string err = null;

            var context = new CityLibraryEntities();

            try
            {
                //  throw new Exception("aaaaaaaaaaaaa");

                context.Users.Add(user);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
                err = ex.Message;
            }

            return err;
        }
예제 #3
0
        public string UpdateUser(User user)
        {
            string err = null;

            var context = new CityLibraryEntities();

            try
            {
                var currentUser = context.Users.SingleOrDefault(u => u.UserID == user.UserID);
                if (currentUser != null)
                {
                    currentUser.FirstName = user.FirstName;
                    currentUser.LastName = user.LastName;

                    context.SaveChanges();
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
                err = ex.Message;
            }

            return err;
        }
예제 #4
0
        public ActionResult Edit(int id, UserModel user)
        {
            if (ModelState.IsValid)
            {
                if (user.BirthDate > DateTime.Now)
                {
                    ModelState.AddModelError("BirthDate", "Jesteś z przyszłości?");
                    return View(user);
                }

                try
                {
                    var dao = new UserService();

                    var u = new User
                    {
                        UserID = id,
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        BirthDate = user.BirthDate,
                        Email = user.Email,
                        Phone = user.Phone,
                        AddDate = user.AddDate,
                        ModifiedDate = DateTime.Now,
                        IsActive = user.IsActive
                    };

                    var err = dao.UpdateUser(u);
                    if (err != null)
                    {
                        ModelState.AddModelError("", "Wystąpił problem podczas dodawania nowego użytkownika.");
                        return View(user);
                    }

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    return View(user);
                }
            }
            else
            {
                return View(user);
            }

            //try
            //{
            //    // TODO: Add update logic here

            //    return RedirectToAction("Index");
            //}
            //catch
            //{
            //    return View();
            //}
        }
예제 #5
0
        public ActionResult Delete(int id, UserModel user)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    var dao = new UserService();

                    var u = new User
                    {
                        UserID = id,
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        BirthDate = user.BirthDate,
                        Email = user.Email,
                        Phone = user.Phone,
                        AddDate = user.AddDate,
                        ModifiedDate = DateTime.Now,
                        IsActive = false
                    };

                    var err = dao.UpdateUser(u);
                    if (err != null)
                    {
                        ModelState.AddModelError("", "Wystąpił problem podczas usuwania użytkownika.");
                        return View(user);
                    }

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    return View(user);
                }
            }
            else
            {
                return View(user);
            }
        }