|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | +using Microsoft.AspNetCore.Mvc; |
| 7 | +using Microsoft.EntityFrameworkCore; |
| 8 | +using AuthWebApplication.Models; |
| 9 | +using AuthWebApplication.Models.Db; |
| 10 | +using AuthWebApplication.Services; |
| 11 | +using Newtonsoft.Json; |
| 12 | +using StackExchange.Redis; |
| 13 | + |
| 14 | +namespace AuthWebApplication.Controllers |
| 15 | +{ |
| 16 | + [Route("api/[controller]")] |
| 17 | + [ApiController] |
| 18 | + public class ApplicationUserTokensController : ControllerBase |
| 19 | + { |
| 20 | + private readonly SecurityDbContext _context; |
| 21 | + private readonly RedisService redisService; |
| 22 | + |
| 23 | + public ApplicationUserTokensController(SecurityDbContext context, RedisService redisService) |
| 24 | + { |
| 25 | + _context = context; |
| 26 | + this.redisService = redisService; |
| 27 | + } |
| 28 | + |
| 29 | + // GET: api/ApplicationUserTokens |
| 30 | + [HttpGet] |
| 31 | + public async Task<ActionResult<IEnumerable<ApplicationUserToken>>> GetApplicationUserTokens() |
| 32 | + { |
| 33 | + return await _context.ApplicationUserTokens.ToListAsync(); |
| 34 | + } |
| 35 | + |
| 36 | + [HttpGet] |
| 37 | + [Route("Search")] |
| 38 | + public async Task<ActionResult<List<object>>> SearchTokens(string keyword) |
| 39 | + { |
| 40 | + var redisValues = await redisService.SearchRedisKeys(keyword); |
| 41 | + |
| 42 | + return Ok(redisValues); |
| 43 | + } |
| 44 | + |
| 45 | + // GET: api/ApplicationUserTokens/5 |
| 46 | + [HttpGet("{id}")] |
| 47 | + public async Task<ActionResult<ApplicationUserToken>> GetApplicationUserToken(string id) |
| 48 | + { |
| 49 | + var applicationUserToken = await _context.ApplicationUserTokens.FindAsync(id); |
| 50 | + |
| 51 | + if (applicationUserToken == null) |
| 52 | + { |
| 53 | + return NotFound(); |
| 54 | + } |
| 55 | + |
| 56 | + return applicationUserToken; |
| 57 | + } |
| 58 | + |
| 59 | + // PUT: api/ApplicationUserTokens/5 |
| 60 | + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 |
| 61 | + [HttpPut("{id}")] |
| 62 | + public async Task<IActionResult> PutApplicationUserToken(string id, ApplicationUserToken applicationUserToken) |
| 63 | + { |
| 64 | + if (id != applicationUserToken.UserId) |
| 65 | + { |
| 66 | + return BadRequest(); |
| 67 | + } |
| 68 | + |
| 69 | + _context.Entry(applicationUserToken).State = EntityState.Modified; |
| 70 | + |
| 71 | + try |
| 72 | + { |
| 73 | + await _context.SaveChangesAsync(); |
| 74 | + } |
| 75 | + catch (DbUpdateConcurrencyException) |
| 76 | + { |
| 77 | + if (!ApplicationUserTokenExists(id)) |
| 78 | + { |
| 79 | + return NotFound(); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + throw; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return NoContent(); |
| 88 | + } |
| 89 | + |
| 90 | + // POST: api/ApplicationUserTokens |
| 91 | + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 |
| 92 | + [HttpPost] |
| 93 | + public async Task<ActionResult<ApplicationUserToken>> PostApplicationUserToken(ApplicationUserToken applicationUserToken) |
| 94 | + { |
| 95 | + _context.ApplicationUserTokens.Add(applicationUserToken); |
| 96 | + try |
| 97 | + { |
| 98 | + await _context.SaveChangesAsync(); |
| 99 | + } |
| 100 | + catch (DbUpdateException) |
| 101 | + { |
| 102 | + if (ApplicationUserTokenExists(applicationUserToken.UserId)) |
| 103 | + { |
| 104 | + return Conflict(); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + throw; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return CreatedAtAction("GetApplicationUserToken", new { id = applicationUserToken.UserId }, applicationUserToken); |
| 113 | + } |
| 114 | + |
| 115 | + // DELETE: api/ApplicationUserTokens/5 |
| 116 | + [HttpDelete("{id}")] |
| 117 | + public async Task<IActionResult> DeleteApplicationUserToken(string id) |
| 118 | + { |
| 119 | + var applicationUserToken = await _context.ApplicationUserTokens.FindAsync(id); |
| 120 | + if (applicationUserToken == null) |
| 121 | + { |
| 122 | + return NotFound(); |
| 123 | + } |
| 124 | + |
| 125 | + _context.ApplicationUserTokens.Remove(applicationUserToken); |
| 126 | + await _context.SaveChangesAsync(); |
| 127 | + |
| 128 | + return NoContent(); |
| 129 | + } |
| 130 | + |
| 131 | + private bool ApplicationUserTokenExists(string id) |
| 132 | + { |
| 133 | + return _context.ApplicationUserTokens.Any(e => e.UserId == id); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments