Advertisement
Ludwiq

Untitled

Oct 27th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1.     [Produces("application/json")]
  2.  
  3.     // [Route("api/[controller]")]
  4.  
  5.     public class WordsController : Controller
  6.  
  7.     {
  8.  
  9.         public AppDbContext _db;
  10.  
  11.         private readonly OpenIddictUserManager<ApplicationUser> _userManager;
  12.  
  13.  
  14.         public WordsController(AppDbContext context, OpenIddictUserManager<ApplicationUser> userManager)
  15.  
  16.         {
  17.  
  18.             _db = context;
  19.  
  20.             _userManager = userManager;
  21.  
  22.         }
  23.  
  24.  
  25.         [HttpGet("api/[controller]")]
  26.  
  27.         public IEnumerable<object> Get()
  28.  
  29.         {
  30.  
  31.             return _db.Words.Include(y => y.Author).Select(x => new WordViewModel
  32.  
  33.             {
  34.  
  35.                 ID = x.ID,
  36.  
  37.                 Title = x.Title,
  38.  
  39.                 AuthorName = x.Author.UserName,
  40.  
  41.                 WhenCreated = x.WhenCreated
  42.  
  43.             }
  44.  
  45.                   ).ToList();
  46.  
  47.         }
  48.  
  49.  
  50.         [HttpPost("api/[controller]")]
  51.  
  52.         [Authorize]
  53.  
  54.         public void Post([FromBody] WordViewModel data)
  55.  
  56.         {
  57.  
  58.             ApplicationUser user = GetCurrentUser();
  59.  
  60.             Word word = new Word
  61.  
  62.             {
  63.  
  64.                 Title = data.Title,
  65.  
  66.                 WhenCreated = DateTime.Now,
  67.  
  68.                 Author = user
  69.  
  70.             };
  71.  
  72.             _db.Words.Add(word);
  73.  
  74.             _db.SaveChanges();
  75.  
  76.         }
  77.  
  78.  
  79.         [HttpPut("api/[controller]")]
  80.  
  81.         public IActionResult Put([FromBody] WordViewModel data)
  82.  
  83.         {
  84.  
  85.             if (data.ID != null)
  86.  
  87.             {
  88.  
  89.                 Word dbWord = _db.Words.Where(x => x.ID == data.ID).FirstOrDefault();
  90.  
  91.                 if (dbWord == null)
  92.  
  93.                 {
  94.  
  95.                     return NotFound();
  96.  
  97.                 }
  98.  
  99.                 if (CheckIfOwner(dbWord.Author))
  100.  
  101.                 {
  102.  
  103.                     return Unauthorized();
  104.  
  105.                 }
  106.  
  107.                 dbWord.Title = data.Title;
  108.  
  109.                 _db.SaveChanges();
  110.  
  111.                 return Ok();
  112.  
  113.             }
  114.  
  115.             else
  116.  
  117.             {
  118.  
  119.                 return BadRequest("ID is required");
  120.  
  121.             }
  122.  
  123.         }
  124.  
  125.  
  126.         [HttpDelete("api/[controller]")]
  127.  
  128.         [Authorize]
  129.  
  130.         public IActionResult Delete(int id)
  131.  
  132.         {
  133.  
  134.             Word dbWord = _db.Words.Where(x => x.ID == id).Include(x => x.Author).FirstOrDefault();
  135.  
  136.             if (dbWord == null)
  137.  
  138.             {
  139.  
  140.                 return NotFound();
  141.  
  142.             }
  143.  
  144.             if (CheckIfOwner(dbWord.Author))
  145.  
  146.             {
  147.  
  148.                 _db.Words.Remove(dbWord);
  149.  
  150.                 _db.SaveChanges();
  151.  
  152.                 return Ok();
  153.  
  154.             }
  155.  
  156.             else
  157.  
  158.             {
  159.  
  160.                 return Unauthorized();
  161.  
  162.             }
  163.  
  164.         }
  165.  
  166.  
  167.         /// <summary>
  168.  
  169.         /// Gets currently logged in user
  170.  
  171.         /// </summary>
  172.  
  173.         public ApplicationUser GetCurrentUser()
  174.  
  175.         {
  176.  
  177.             return _userManager.Users.FirstOrDefault(x => x.NormalizedUserName == User.Identity.Name);
  178.  
  179.         }
  180.  
  181.  
  182.         /// <summary>
  183.  
  184.         /// Checks if user is the same as currently logged in one
  185.  
  186.         /// </summary>
  187.  
  188.         public bool CheckIfOwner(ApplicationUser userToCompare)
  189.  
  190.         {
  191.  
  192.             ApplicationUser currentUser = GetCurrentUser();
  193.  
  194.             return currentUser.Id == userToCompare.Id;
  195.  
  196.         }
  197.  
  198.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement