props/Props/server/Controllers/UserConfigurationsController.cs

82 lines
3.4 KiB
C#

using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Props.Data;
using Props.Models;
using Props.Shared.Models;
namespace Props.Server.Controllers
{
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class UserConfigurationsController : ControllerBase
{
private ILogger<UserConfigurationsController> logger;
private UserManager<ApplicationUser> userManager;
private ApplicationDbContext dbContext;
public UserConfigurationsController(UserManager<ApplicationUser> userManager, ApplicationDbContext dbContext, ILogger<UserConfigurationsController> logger)
{
this.userManager = userManager;
this.dbContext = dbContext;
this.logger = logger;
}
[HttpGet]
public async Task<IActionResult> GetSearchOutline() {
ApplicationUser userModel = await userManager.GetUserAsync(User);
return Ok(userModel.SearchOutline);
}
[HttpGet]
public async Task<IActionResult> GetResultsPreferences() {
ApplicationUser userModel = await userManager.GetUserAsync(User);
return Ok(userModel.ResultsPreferences);
}
[HttpGet]
public async Task<IActionResult> GetApplicationPreferences() {
ApplicationUser userModel = await userManager.GetUserAsync(User);
logger.LogInformation(JsonSerializer.Serialize(userModel.ApplicationPreferences));
return Ok(userModel.ApplicationPreferences);
}
[HttpPut]
public async Task<IActionResult> PutSearchOutline(SearchOutline searchOutline) {
ApplicationUser userModel = await userManager.GetUserAsync(User);
if (userModel.SearchOutline.Id != searchOutline.Id || userModel.Id != searchOutline.ApplicationUserId) {
return BadRequest();
}
dbContext.Entry(userModel.SearchOutline).CurrentValues.SetValues(searchOutline);
await userManager.UpdateAsync(userModel);
return NoContent();
}
[HttpPut]
public async Task<IActionResult> PutResultsPreferences(ResultsPreferences resultsPreferences) {
ApplicationUser userModel = await userManager.GetUserAsync(User);
if (userModel.ResultsPreferences.Id != resultsPreferences.Id || userModel.Id != resultsPreferences.ApplicationUserId) {
return BadRequest();
}
dbContext.Entry(userModel.ResultsPreferences).CurrentValues.SetValues(resultsPreferences);
await userManager.UpdateAsync(userModel);
return NoContent();
}
[HttpPut]
public async Task<IActionResult> PutApplicationPreferences(ApplicationPreferences applicationPreferences) {
ApplicationUser userModel = await userManager.GetUserAsync(User);
logger.LogInformation(JsonSerializer.Serialize(applicationPreferences));
if (userModel.ApplicationPreferences.Id != applicationPreferences.Id || userModel.Id != applicationPreferences.ApplicationUserId) {
return BadRequest();
}
dbContext.Entry(userModel.ApplicationPreferences).CurrentValues.SetValues(applicationPreferences);
await userManager.UpdateAsync(userModel);
return NoContent();
}
}
}