refactoring for integration
This commit is contained in:
parent
80144bf9fc
commit
070f41ac7e
48 changed files with 100 additions and 46 deletions
89
src/Yavsc.Api/Controllers/accounting/AccountController.cs
Normal file
89
src/Yavsc.Api/Controllers/accounting/AccountController.cs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Api.Helpers;
|
||||
using Yavsc.Server.Helpers;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Yavsc.WebApi.Controllers
|
||||
{
|
||||
[Route("~/api/account")]
|
||||
[Authorize("ApiScope")]
|
||||
public class ApiAccountController : Controller
|
||||
{
|
||||
readonly ApplicationDbContext _dbContext;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public ApiAccountController(
|
||||
ILoggerFactory loggerFactory, ApplicationDbContext dbContext)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger(nameof(ApiAccountController));
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
public async Task<IActionResult> Me()
|
||||
{
|
||||
if (User == null)
|
||||
return new BadRequestObjectResult(
|
||||
new { error = "user not found" });
|
||||
var uid = User.GetUserId();
|
||||
Debug.Assert(uid != null, "uid is null");
|
||||
var userData = await GetUserData(uid);
|
||||
Debug.Assert(userData != null, "userData is null");
|
||||
var user = new Yavsc.Models.Auth.Me(userData.Id, userData.UserName, userData.Email,
|
||||
userData.Avatar,
|
||||
userData.PostalAddress, userData.DedicatedGoogleCalendar);
|
||||
|
||||
var userRoles = _dbContext.UserRoles.Where(u => u.UserId == uid).Select(r => r.RoleId).ToArray();
|
||||
|
||||
IdentityRole[] roles = _dbContext.Roles.Where(r => userRoles.Contains(r.Id)).ToArray();
|
||||
|
||||
user.Roles = roles.Select(r => r.Name).ToArray();
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
private async Task<ApplicationUser> GetUserData(string uid)
|
||||
{
|
||||
return await _dbContext.Users
|
||||
.Include(u => u.PostalAddress)
|
||||
.Include(u => u.AccountBalance)
|
||||
.FirstAsync(u => u.Id == uid);
|
||||
}
|
||||
|
||||
[HttpGet("myhost")]
|
||||
public IActionResult MyHost ()
|
||||
{
|
||||
return Ok(new { host = Request.ForwardedFor() });
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updates the avatar
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost("~/api/set-avatar")]
|
||||
public async Task<IActionResult> SetAvatar()
|
||||
{
|
||||
var user = await GetUserData(User.GetUserId());
|
||||
if (Request.Form.Files.Count!=1)
|
||||
return new BadRequestResult();
|
||||
if (!Request.Form.Files[0].ContentType.StartsWith("image/png"))
|
||||
return new BadRequestResult();
|
||||
|
||||
var info = user.ReceiveAvatar(Request.Form.Files[0]);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
return Ok(info);
|
||||
}
|
||||
|
||||
[HttpGet("identity")]
|
||||
public async Task<IActionResult> Identity()
|
||||
{
|
||||
return Json(User.Claims.Select(c=>new {c.Type, c.Value}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,166 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Abstract.Identity;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json"),Authorize("AdministratorOnly")]
|
||||
[Route("api/users")]
|
||||
public class ApplicationUserApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public ApplicationUserApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/ApplicationUserApi
|
||||
[HttpGet]
|
||||
public IEnumerable<UserInfo> GetApplicationUser(int skip=0, int take = 25)
|
||||
{
|
||||
return _context.Users.Skip(skip).Take(take)
|
||||
.Select(u=> new UserInfo{
|
||||
UserId = u.Id,
|
||||
UserName = u.UserName,
|
||||
Avatar = u.Avatar});
|
||||
}
|
||||
|
||||
[HttpGet("search/{pattern}")]
|
||||
public IEnumerable<UserInfo> SearchApplicationUser(string pattern, int skip=0, int take = 25)
|
||||
{
|
||||
return _context.Users.Where(u => u.UserName.Contains(pattern))
|
||||
.Skip(skip).Take(take)
|
||||
.Select(u=> new UserInfo {
|
||||
UserId = u.Id,
|
||||
UserName = u.UserName,
|
||||
Avatar = u.Avatar });
|
||||
}
|
||||
|
||||
// GET: api/ApplicationUserApi/5
|
||||
[HttpGet("{id}", Name = "GetApplicationUser")]
|
||||
public IActionResult GetApplicationUser([FromRoute] string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
ApplicationUser applicationUser = _context.Users.Single(m => m.Id == id);
|
||||
|
||||
if (applicationUser == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(applicationUser);
|
||||
}
|
||||
|
||||
// PUT: api/ApplicationUserApi/5
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult PutApplicationUser(string id, [FromBody] ApplicationUser applicationUser)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != applicationUser.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(applicationUser).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ApplicationUserExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/ApplicationUserApi
|
||||
[HttpPost]
|
||||
public IActionResult PostApplicationUser([FromBody] ApplicationUser applicationUser)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Users.Add(applicationUser);
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (ApplicationUserExists(applicationUser.Id))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetApplicationUser", new { id = applicationUser.Id }, applicationUser);
|
||||
}
|
||||
|
||||
// DELETE: api/ApplicationUserApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteApplicationUser(string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
ApplicationUser applicationUser = _context.Users.Single(m => m.Id == id);
|
||||
if (applicationUser == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/Yavsc.Api/Controllers/accounting/ProfileApiController.cs
Normal file
41
src/Yavsc.Api/Controllers/accounting/ProfileApiController.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Abstract.Identity;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.ApiControllers.accounting
|
||||
{
|
||||
[Route("~/api/profile")]
|
||||
public class ProfileApiController: Controller
|
||||
{
|
||||
readonly UserManager<ApplicationUser> _userManager;
|
||||
readonly ApplicationDbContext _dbContext;
|
||||
public ProfileApiController(ApplicationDbContext dbContext, UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
[HttpGet("{allow}",Name ="setmonthlyemail")]
|
||||
public async Task<object> SetMonthlyEmail(bool allow)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(User.GetUserId());
|
||||
user.AllowMonthlyEmail = allow;
|
||||
_dbContext.SaveChanges(User.GetUserId());
|
||||
return Ok(new { monthlyEmailPrefSaved = allow });
|
||||
}
|
||||
|
||||
[HttpGet("userhint/{name}")]
|
||||
public UserInfo[] GetUserHint(string name)
|
||||
{
|
||||
return _dbContext.Users.Where(u=>u.UserName.IndexOf(name)>0)
|
||||
.Select(u=>new UserInfo(u.Id, u.UserName, u.Email, u.Avatar))
|
||||
.Take(10).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue