2024-01-29 18:36:38 -05:00
|
|
|
using comissions.app.api.Extensions;
|
2024-01-28 06:25:10 -05:00
|
|
|
using ArtPlatform.Database;
|
2024-01-29 18:36:38 -05:00
|
|
|
using comissions.app.database;
|
2024-01-28 06:25:10 -05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2024-01-28 16:42:56 -05:00
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
2024-01-28 06:25:10 -05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-01-28 16:42:56 -05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-01-28 06:25:10 -05:00
|
|
|
|
2024-01-29 18:36:38 -05:00
|
|
|
namespace comissions.app.api.Controllers;
|
2024-01-28 06:25:10 -05:00
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Authorize("admin")]
|
|
|
|
[Route("api/admin/[controller]")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public class AdminSellersController:ControllerBase
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
|
|
|
private readonly ApplicationDbContext _dbContext;
|
|
|
|
|
|
|
|
public AdminSellersController(ApplicationDbContext dbContext)
|
|
|
|
{
|
|
|
|
_dbContext = dbContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> GetSellers(string search="", int offset = 0, int pageSize = 10)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var sellers = await _dbContext.UserSellerProfiles.Include(x=>x.User)
|
|
|
|
.Where(x=>x.User.DisplayName.Contains(search) || x.User.Email.Contains(search))
|
|
|
|
.Skip(offset).Take(pageSize).ToListAsync();
|
|
|
|
return Ok(sellers);
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("Count")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> GetSellersCount(string search="")
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var result = await _dbContext.UserSellerProfiles.Include(x=>x.User)
|
|
|
|
.Where(x=>x.User.DisplayName.Contains(search) || x.User.Email.Contains(search))
|
|
|
|
.CountAsync();
|
|
|
|
return Ok(result);
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{sellerId:int}")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> GetSeller(int sellerId)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var seller = await _dbContext.UserSellerProfiles.Include(x=>x.User)
|
|
|
|
.FirstOrDefaultAsync(x=>x.Id==sellerId);
|
|
|
|
|
|
|
|
if (seller == null)
|
|
|
|
return NotFound("Seller not found.");
|
|
|
|
|
|
|
|
return Ok(seller);
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{sellerId:int}/Orders")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> GetSellerOrders(int sellerId)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var seller = _dbContext.UserSellerProfiles.Include(x=>x.User)
|
|
|
|
.FirstOrDefault(x=>x.Id==sellerId);
|
|
|
|
|
|
|
|
if (seller == null)
|
|
|
|
return NotFound("Seller not found.");
|
|
|
|
|
|
|
|
var orders = await _dbContext.SellerServiceOrders.Where(x=>x.SellerId==sellerId).ToListAsync();
|
|
|
|
return Ok(orders);
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut("{sellerId:int}/Suspend")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> SuspendSeller(int sellerId, [FromQuery]string reason, [FromQuery]int days)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var seller = _dbContext.UserSellerProfiles.FirstOrDefault(x=>x.Id==sellerId);
|
|
|
|
|
|
|
|
if (seller == null)
|
|
|
|
return NotFound("Seller not found.");
|
|
|
|
|
|
|
|
if (seller.Suspended)
|
|
|
|
return BadRequest("Seller is already suspended.");
|
|
|
|
|
|
|
|
seller.Suspended = true;
|
|
|
|
seller.SuspendedDate = DateTime.UtcNow;
|
|
|
|
seller.UnsuspendDate = DateTime.UtcNow.AddDays(days);
|
|
|
|
seller.SuspendedReason = reason;
|
|
|
|
seller.SuspendAdminId = User.GetUserId();
|
|
|
|
_dbContext.UserSellerProfiles.Update(seller);
|
|
|
|
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return Ok();
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut("{sellerId:int}/Unsuspend")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> UnsuspendSeller(int sellerId)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var seller = _dbContext.UserSellerProfiles.FirstOrDefault(x=>x.Id==sellerId);
|
|
|
|
|
|
|
|
if (seller == null)
|
|
|
|
return NotFound("Seller not found.");
|
|
|
|
|
|
|
|
if (!seller.Suspended)
|
|
|
|
return BadRequest("Seller is not suspended.");
|
|
|
|
|
|
|
|
seller.Suspended = false;
|
|
|
|
seller.SuspendedDate = null;
|
|
|
|
seller.UnsuspendDate = null;
|
|
|
|
seller.SuspendedReason = null;
|
|
|
|
seller.SuspendAdminId = null;
|
|
|
|
_dbContext.UserSellerProfiles.Update(seller);
|
|
|
|
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return Ok();
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut("{sellerId:int}/Terminate")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> TerminateSeller(int sellerId)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var seller = _dbContext.UserSellerProfiles.FirstOrDefault(x=>x.Id==sellerId);
|
|
|
|
|
|
|
|
if (seller == null)
|
|
|
|
return NotFound("Seller not found.");
|
|
|
|
|
|
|
|
if (!seller.Suspended)
|
|
|
|
return BadRequest("Seller is not suspended.");
|
|
|
|
|
|
|
|
_dbContext.UserSellerProfiles.Remove(seller);
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return Ok();
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut("{sellerId:int}/SetBiography")]
|
2024-01-28 16:42:56 -05:00
|
|
|
public async Task<IActionResult> SetBiography(int sellerId, [FromBody]string biography)
|
2024-01-28 06:25:10 -05:00
|
|
|
{
|
2024-01-28 16:42:56 -05:00
|
|
|
var seller = _dbContext.UserSellerProfiles.FirstOrDefault(x=>x.Id==sellerId);
|
|
|
|
|
|
|
|
if (seller == null)
|
|
|
|
return NotFound("Seller not found.");
|
|
|
|
|
|
|
|
if (!seller.Suspended)
|
|
|
|
return BadRequest("Seller is not suspended.");
|
|
|
|
|
|
|
|
seller.Biography = biography;
|
|
|
|
_dbContext.UserSellerProfiles.Update(seller);
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return Ok();
|
2024-01-28 06:25:10 -05:00
|
|
|
}
|
|
|
|
}
|