Esempio n. 1
0
        public static void AddUser(User u)
        {
            foreach (var user in GetAllUsers())
            {
                if (string.Equals(user.Login, u.Login, StringComparison.CurrentCultureIgnoreCase))
                {
                    throw new Exception("The user with such Login already exists.");
                }
                if (string.Equals(user.Email, u.Email, StringComparison.CurrentCultureIgnoreCase))
                {
                    throw new Exception("The user with such Email already exists.");
                }
            }

            List<User> users;
            using (var sr = new StreamReader(FilePath))
            {
                users = JsonConvert.DeserializeObject<List<User>>(sr.ReadToEnd());
            }
            users.Add(u);
            using (var sw = new StreamWriter(FilePath, false))
            {
                sw.WriteLine(JsonConvert.SerializeObject(users));
            }
        }
Esempio n. 2
0
        public static void EditUser(User u)
        {
            var user = GetUser(u.Login);
            if (user == null)
            {
                return;
            }

            List<User> users;
            using (var sr = new StreamReader(FilePath))
            {
                users = JsonConvert.DeserializeObject<List<User>>(sr.ReadToEnd());
            }
            users = users.Where(us => !string.Equals(us.Login, u.Login, StringComparison.CurrentCultureIgnoreCase)).ToList();
            users.Add(u);
            using (var sw = new StreamWriter(FilePath, false))
            {
                sw.WriteLine(JsonConvert.SerializeObject(users));
            }
        }
Esempio n. 3
0
 public ActionResult Edit(User user)
 {
     DataManager.EditUser(user);
     return RedirectToAction("Index", "User");
 }
Esempio n. 4
0
 public ActionResult Create(User user)
 {
     DataManager.AddUser(user);
     return RedirectToAction("Index", "User");
 }