yavsc/src/Yavsc.Api/Controllers/accounting/ApplicationUserApiController.cs

166 lines
4.7 KiB
C#
Raw Normal View History

2019-01-01 16:28:47 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Yavsc.Abstract.Identity;
2023-03-19 17:57:55 +00:00
using Yavsc.Helpers;
2019-01-01 16:28:47 +00:00
using Yavsc.Models;
using Yavsc.Server.Helpers;
2019-01-01 16:28:47 +00:00
namespace Yavsc.Controllers
{
2025-08-18 11:27:13 +01:00
[Produces("application/json"),Authorize("AdministratorOnly")]
2019-01-01 16:28:47 +00:00
[Route("api/users")]
public class ApplicationUserApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
2019-01-01 16:28:47 +00:00
public ApplicationUserApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/ApplicationUserApi
[HttpGet]
2023-04-05 22:29:40 +01:00
public IEnumerable<UserInfo> GetApplicationUser(int skip=0, int take = 25)
2019-01-01 16:28:47 +00:00
{
2023-04-05 22:29:40 +01:00
return _context.Users.Skip(skip).Take(take)
.Select(u=> new UserInfo{
2019-06-22 19:34:39 +01:00
UserId = u.Id,
UserName = u.UserName,
2023-04-05 22:29:40 +01:00
Avatar = u.Avatar});
2019-06-22 19:34:39 +01:00
}
[HttpGet("search/{pattern}")]
2023-04-05 22:29:40 +01:00
public IEnumerable<UserInfo> SearchApplicationUser(string pattern, int skip=0, int take = 25)
2019-06-22 19:34:39 +01:00
{
return _context.Users.Where(u => u.UserName.Contains(pattern))
2023-04-05 22:29:40 +01:00
.Skip(skip).Take(take)
2019-06-22 19:34:39 +01:00
.Select(u=> new UserInfo {
UserId = u.Id,
UserName = u.UserName,
Avatar = u.Avatar });
2019-01-01 16:28:47 +00:00
}
// GET: api/ApplicationUserApi/5
[HttpGet("{id}", Name = "GetApplicationUser")]
public IActionResult GetApplicationUser([FromRoute] string id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2023-03-19 17:57:55 +00:00
ApplicationUser applicationUser = _context.Users.Single(m => m.Id == id);
2019-01-01 16:28:47 +00:00
if (applicationUser == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
return Ok(applicationUser);
}
// PUT: api/ApplicationUserApi/5
[HttpPut("{id}")]
public IActionResult PutApplicationUser(string id, [FromBody] ApplicationUser applicationUser)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
if (id != applicationUser.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-01-01 16:28:47 +00:00
}
_context.Entry(applicationUser).State = EntityState.Modified;
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!ApplicationUserExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status204NoContent);
2019-01-01 16:28:47 +00:00
}
// POST: api/ApplicationUserApi
[HttpPost]
public IActionResult PostApplicationUser([FromBody] ApplicationUser applicationUser)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
_context.Users.Add(applicationUser);
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateException)
{
if (ApplicationUserExists(applicationUser.Id))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
return CreatedAtRoute("GetApplicationUser", new { id = applicationUser.Id }, applicationUser);
}
// DELETE: api/ApplicationUserApi/5
[HttpDelete("{id}")]
public IActionResult DeleteApplicationUser(string id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
ApplicationUser applicationUser = _context.Users.Single(m => m.Id == id);
if (applicationUser == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
_context.Users.Remove(applicationUser);
_context.SaveChanges(User.GetUserId());
return Ok(applicationUser);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool ApplicationUserExists(string id)
{
return _context.Users.Count(e => e.Id == id) > 0;
}
}
}