2021-08-17 07:59:01 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2021-08-05 06:22:19 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-17 07:59:01 +00:00
|
|
|
using Props.Data;
|
2021-08-05 06:22:19 +00:00
|
|
|
using Props.Models.Search;
|
2021-08-17 07:59:01 +00:00
|
|
|
using Props.Models.User;
|
|
|
|
using Props.Shop.Framework;
|
2021-08-05 06:22:19 +00:00
|
|
|
|
|
|
|
namespace Props.Controllers
|
|
|
|
{
|
|
|
|
public class SearchOutlineController : ApiControllerBase
|
|
|
|
{
|
2021-08-17 07:59:01 +00:00
|
|
|
private ApplicationDbContext dbContext;
|
|
|
|
private UserManager<ApplicationUser> userManager;
|
|
|
|
public SearchOutlineController(UserManager<ApplicationUser> userManager, ApplicationDbContext dbContext)
|
|
|
|
{
|
|
|
|
this.userManager = userManager;
|
|
|
|
this.dbContext = dbContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{name:required}")]
|
|
|
|
public async Task<IActionResult> DeleteSearchOutline(string name)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
{
|
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
SearchOutlinePreferences searchOutlinePrefs = user.searchOutlinePreferences;
|
|
|
|
searchOutlinePrefs.SearchOutlines.Remove(searchOutlinePrefs.SearchOutlines.Single((outline) => name.Equals(outline.Name)));
|
|
|
|
await userManager.UpdateAsync(user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{name:required}")]
|
|
|
|
public async Task<IActionResult> PostSearchOutline(string name)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(name)) return BadRequest();
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
SearchOutline searchOutline = user.searchOutlinePreferences.SearchOutlines.SingleOrDefault((outline) => name.Equals(outline.Name));
|
|
|
|
if (searchOutline != null) return BadRequest();
|
|
|
|
searchOutline = new SearchOutline();
|
|
|
|
searchOutline.Name = name;
|
|
|
|
user.searchOutlinePreferences.SearchOutlines.Add(searchOutline);
|
|
|
|
await userManager.UpdateAsync(user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{name:required}/Filters")]
|
|
|
|
public async Task<IActionResult> PutFilters(string name, Filters filters)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(name)) return BadRequest();
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
SearchOutline searchOutline = await GetSearchOutlineByName(name);
|
|
|
|
if (searchOutline == null) return BadRequest();
|
|
|
|
searchOutline.Filters = filters;
|
|
|
|
await userManager.UpdateAsync(user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{outlineName:required}/DisabledShops")]
|
|
|
|
public async Task<IActionResult> PutShopSelection(string outlineName, ISet<string> disabledShops)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(outlineName)) return BadRequest();
|
|
|
|
if (disabledShops == null) return BadRequest();
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
SearchOutline searchOutline = await GetSearchOutlineByName(outlineName);
|
|
|
|
if (searchOutline == null) return BadRequest();
|
|
|
|
|
|
|
|
searchOutline.DisabledShops.Clear();
|
|
|
|
searchOutline.DisabledShops.UnionWith(disabledShops);
|
|
|
|
await userManager.UpdateAsync(user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{oldName:required}/Name/{newName:required}")]
|
|
|
|
public async Task<IActionResult> PutName(string oldName, string newName)
|
|
|
|
{
|
|
|
|
if (oldName == newName) return BadRequest();
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
SearchOutline outline = await GetSearchOutlineByName(oldName);
|
|
|
|
if (outline == null) return BadRequest();
|
|
|
|
if (user.searchOutlinePreferences.SearchOutlines.Any((outline) => outline.Name.Equals(newName))) return BadRequest();
|
|
|
|
outline.Name = newName;
|
|
|
|
if (user.searchOutlinePreferences.NameOfLastUsed == oldName)
|
|
|
|
{
|
|
|
|
user.searchOutlinePreferences.NameOfLastUsed = newName;
|
|
|
|
}
|
|
|
|
await userManager.UpdateAsync(user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{name:required}/LastUsed")]
|
|
|
|
public async Task<IActionResult> PutLastUsed(string name)
|
|
|
|
{
|
|
|
|
SearchOutline outline = await GetSearchOutlineByName(name);
|
|
|
|
if (outline == null) return BadRequest();
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
user.searchOutlinePreferences.NameOfLastUsed = name;
|
|
|
|
await userManager.UpdateAsync(user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{name:required}/Filters")]
|
|
|
|
public async Task<IActionResult> GetFilters(string name)
|
|
|
|
{
|
|
|
|
Filters filters = (await GetSearchOutlineByName(name))?.Filters;
|
|
|
|
if (filters == null) return BadRequest();
|
|
|
|
return Ok(filters);
|
|
|
|
}
|
2021-08-05 06:22:19 +00:00
|
|
|
|
2021-08-17 07:59:01 +00:00
|
|
|
[HttpGet]
|
|
|
|
[Authorize]
|
|
|
|
[Route("{name:required}/DisabledShops")]
|
|
|
|
public async Task<IActionResult> GetDisabledShops(string name)
|
|
|
|
{
|
|
|
|
SearchOutline searchOutline = await GetSearchOutlineByName(name);
|
|
|
|
if (searchOutline == null)
|
|
|
|
{
|
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
return Ok(searchOutline.DisabledShops);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
[Authorize]
|
|
|
|
[Route("Names")]
|
|
|
|
public async Task<IActionResult> GetSearchOutlineNames()
|
|
|
|
{
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
return Ok(user.searchOutlinePreferences.SearchOutlines.Select((outline, Index) => outline.Name));
|
|
|
|
}
|
2021-08-05 06:22:19 +00:00
|
|
|
|
2021-08-17 07:59:01 +00:00
|
|
|
[HttpGet]
|
|
|
|
[Authorize]
|
|
|
|
[Route("LastUsed")]
|
|
|
|
public async Task<IActionResult> GetLastSearchOutlineName()
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
2021-08-17 07:59:01 +00:00
|
|
|
SearchOutline searchOutline = await GetLastUsedSearchOutline();
|
|
|
|
|
|
|
|
return Ok(searchOutline?.Name);
|
2021-08-05 06:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-08-17 07:59:01 +00:00
|
|
|
[Route("DefaultDisabledShops")]
|
|
|
|
public IActionResult GetDefaultDisabledShops()
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
2021-08-17 07:59:01 +00:00
|
|
|
return Ok(new SearchOutline.ShopSelector());
|
2021-08-05 06:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-08-17 07:59:01 +00:00
|
|
|
[Route("DefaultFilters")]
|
|
|
|
public IActionResult GetDefaultFilter()
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
2021-08-17 07:59:01 +00:00
|
|
|
return Ok(new Filters());
|
2021-08-05 06:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-08-17 07:59:01 +00:00
|
|
|
[Route("DefaultName")]
|
|
|
|
public async Task<IActionResult> GetDefaultName()
|
|
|
|
{
|
|
|
|
string nameTemplate = "Search Outline {0}";
|
|
|
|
if (User.Identity.IsAuthenticated)
|
|
|
|
{
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
int number = user.searchOutlinePreferences.SearchOutlines.Count;
|
|
|
|
string name = null;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
name = string.Format(nameTemplate, number);
|
|
|
|
number += 1;
|
|
|
|
} while (user.searchOutlinePreferences.SearchOutlines.Any((outline) => name.Equals(outline.Name)));
|
|
|
|
return Ok(name);
|
|
|
|
}
|
|
|
|
return Ok("Search Outline");
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<SearchOutline> GetLastUsedSearchOutline()
|
|
|
|
{
|
|
|
|
if (!User.Identity.IsAuthenticated) return null;
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
return user.searchOutlinePreferences.SearchOutlines.SingleOrDefault((outline) => outline.Name.Equals(user.searchOutlinePreferences.NameOfLastUsed));
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<SearchOutline> GetSearchOutlineByName(string name)
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
2021-08-17 07:59:01 +00:00
|
|
|
if (name == null) throw new ArgumentNullException("name");
|
|
|
|
if (!User.Identity.IsAuthenticated) return null;
|
|
|
|
ApplicationUser user = await userManager.GetUserAsync(User);
|
|
|
|
return user.searchOutlinePreferences.SearchOutlines.SingleOrDefault(outline => outline.Name.Equals(name));
|
2021-08-05 06:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|