예제 #1
0
 public IActionResult Create(User user)
 {
     if (string.IsNullOrWhiteSpace(user.Name) || user.Age <= 0)
     {
         return(BadRequest("Не введено имя пользователи или возраст равен или менее нуля"));
     }
     else
     {
         _db.Add(user);
         return(RedirectToAction("Index"));
     }
 }
예제 #2
0
        static void Main(string[] args)
        {
            DbActions db  = new DbActions();
            int       Id  = 0;
            int       chs = 0;

            while (true)
            {
                Console.WriteLine("Welcome To The Hell Hight Level");
                db.Select().ForEach(s =>
                {
                    Console.WriteLine($"Id = {s.Id}\nFullName = {s.FullName}\nAge = {s.Age}");
                });
                Console.Write("You can add one enter 1 or Select one enter 2:");
                chs = int.Parse(Console.ReadLine());
                if (chs == 1)
                {
                    Student st = new Student();
                    Console.Write("Enter FullName:");
                    st.FullName = Console.ReadLine();
                    Console.Write("Ener Age:");
                    st.Age = int.Parse(Console.ReadLine());
                    db.Add(st);
                }
                else if (chs == 2)
                {
                    Console.WriteLine("Select one from sight(I need just Id) enter 2:");
                    try
                    {
                        Id = int.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                    Console.Write("What will you do?\nYou can delete this poor student or update his or her values(I know you are cruel and you are in dark side)\n1 for delete\n2 for update\n:");
                    try
                    {
                        chs = int.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Somethink went wrong please try again");
                        continue;
                    }
                    if (chs == 1)
                    {
                        db.Delete(Id);
                        Console.Clear();
                        Console.WriteLine("Congratulation my hopes were not rejected");
                        continue;
                    }
                    else if (chs == 2)
                    {
                        Student st = new Student();
                        st.Id = Id;
                        Console.WriteLine("You are so soft. ok Enter FullName:");
                        st.FullName = Console.ReadLine();
                        bool z = true;
                        while (z)
                        {
                            Console.WriteLine("Enter Age:");
                            try
                            {
                                st.Age = int.Parse(Console.ReadLine());
                                z      = false;
                            }
                            catch (Exception ex)
                            {
                                Console.Clear();
                                Console.Write("There was an error try it again!\n");
                                continue;
                            }
                        }
                        db.Update(st);
                        Console.Clear();
                    }
                }
            }
        }
예제 #3
0
        static void Main()
        {
            DbActions db = new DbActions();

            while (true)
            {
                List <User> user = db.Select();
                foreach (User e in user)
                {
                    Console.WriteLine($"Id - {e.Id}, Name - {e.Name}, Age - {e.Age}");
                }

                Console.WriteLine(@"
Выберите действие:
1.Добавить элемент
2.Изменить элемент
3.Удалить элемент
4.Выйти из приложения");
                string ans = Console.ReadLine();
                switch (ans)
                {
                case "1":     // Добавление элемента
                    User newUser = new User();
                    try
                    {
                        Console.Write("Введите имя ");
                        newUser.Name = Console.ReadLine();
                        Console.Write("\nВведите возраст ");
                        newUser.Age = int.Parse(Console.ReadLine());
                        db.Add(newUser);
                    }
                    catch
                    {
                        Console.WriteLine("Некорректный ввод");
                        Console.ReadKey();
                    }
                    Console.Clear();
                    break;

                case "2":     // Изменение элемента
                    try
                    {
                        Console.WriteLine("Введите id элемента");
                        int id = int.Parse(Console.ReadLine());
                        Console.WriteLine("Введите новое имя элемента, а следом новый возраст");
                        string name        = Console.ReadLine();
                        int    age         = int.Parse(Console.ReadLine() ?? string.Empty);
                        User   newUserData = new() { Name = name, Age = age };
                        db.Edit(id, newUserData);
                    }
                    catch
                    {
                        Console.WriteLine("Некорректный ввод");
                        Console.ReadKey();
                    }
                    Console.Clear();
                    break;

                case "3":     // Удаление элемента
                    Console.WriteLine("Введите номер элемента");
                    try
                    {
                        int id = int.Parse(Console.ReadLine() ?? string.Empty);
                        db.Delete(id);
                    }
                    catch
                    {
                        Console.WriteLine("Некорректный ввод");
                        Console.ReadKey();
                    }
                    Console.Clear();
                    break;

                case "4":     // Выход из приложения
                    Console.WriteLine("Приложение закроется через 2 сек");
                    Thread.Sleep(1500);
                    Environment.Exit(0);
                    break;

                default:
                    Console.Clear();
                    Console.WriteLine("Некорректный ввод");
                    Thread.Sleep(500);
                    Console.Clear();
                    break;
                }
            }
        }
예제 #4
0
        public async Task <ActionResult <User> > PostUser(User user)
        {
            _db.Add(user);

            return(CreatedAtAction("GetUser", new { id = user.Id }, user));
        }