Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Mvc;
  7. using React_01.Models;
  8.  
  9. namespace React_01.Controllers
  10. {
  11.     public class HomeController : Controller
  12.     {
  13.         private static readonly IList<UserModel> users;
  14.         public IActionResult Index()
  15.         {
  16.             return View();
  17.         }
  18.  
  19.         public IActionResult About()
  20.         {
  21.             ViewData["Message"] = "Your application description page.";
  22.  
  23.             return View();
  24.         }
  25.  
  26.         public IActionResult Contact()
  27.         {
  28.             ViewData["Message"] = "Your contact page.";
  29.  
  30.             return View();
  31.         }
  32.  
  33.         public IActionResult Error()
  34.         {
  35.             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  36.         }
  37.  
  38.         // --- Login ---
  39.         [Route("Home/login")]
  40.         [HttpPost]
  41.         public ActionResult Login(UserModel user)
  42.         {
  43.             var userBuff = users.FirstOrDefault(item => item.Name == user.Name);
  44.             if (userBuff == null)
  45.             {
  46.                 return Content("User not found");
  47.             }
  48.             else
  49.             {
  50.                 if (userBuff.Password == user.Password)
  51.                 {
  52.                     //   return Content("User logged");
  53.                     // return Json(user);
  54.                         return Json(new{succes = true, User = user, message = user.Name + " " + user.Id});
  55.                 }
  56.                 else
  57.                 {
  58.                     return Content("Wrong password");
  59.                 }
  60.  
  61.             }
  62.         }
  63.  
  64.         [HttpGet]
  65.         public JsonResult GetOrderForAdd()
  66.         {
  67.             return Json("test");
  68.         }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.         // --- AddUser ---
  75.         [Route("users/new")]
  76.         [HttpPost]
  77.         public ActionResult AddUser(UserModel user)
  78.         {
  79.             user.Id = users.Count + 1;
  80.             users.Add(user);
  81.             return Content("User added");
  82.         }
  83.  
  84.         // --- Users ---
  85.         [Route("users")]
  86.         [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
  87.         public ActionResult Users()
  88.         {
  89.             return Json(users);
  90.         }
  91.  
  92.         static HomeController()
  93.         {
  94.             users = new List<UserModel>
  95.             {
  96.                 new UserModel
  97.                 {
  98.                     Id = 1,
  99.                     Name = "juan",
  100.                     Password = "12345"
  101.                 },
  102.                 new UserModel
  103.                 {
  104.                     Id = 2,
  105.                     Name = "juan2",
  106.                     Password = "1234567"
  107.                 },
  108.                 new UserModel
  109.                 {
  110.                     Id = 3,
  111.                     Name = "juan3",
  112.                     Password = "999999999"
  113.                 },
  114.             };
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement