mirror of
https://github.com/D4M13N-D3V/comissions-app-core-api.git
synced 2025-03-14 10:04:55 +00:00
feat: removed service controller
This commit is contained in:
parent
ca2af66c62
commit
8f50f3b6c3
@ -154,7 +154,7 @@ public class SellerProfileController : Controller
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Route("Portfolio")]
|
[Route("Portfolio")]
|
||||||
[Authorize("write:seller-profile")]
|
[Authorize("write:seller-profile")]
|
||||||
public async Task<IActionResult> AddPortfolio([FromBody]IFormFile newImage)
|
public async Task<IActionResult> AddPortfolio()
|
||||||
{
|
{
|
||||||
var userId = User.GetUserId();
|
var userId = User.GetUserId();
|
||||||
var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
||||||
@ -165,7 +165,7 @@ public class SellerProfileController : Controller
|
|||||||
|
|
||||||
if(existingSellerProfile.Suspended)
|
if(existingSellerProfile.Suspended)
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
var url = await _storageService.UploadImageAsync(newImage, Guid.NewGuid().ToString());
|
var url = await _storageService.UploadImageAsync(HttpContext.Request.Body, Guid.NewGuid().ToString());
|
||||||
var portfolio = new SellerProfilePortfolioPiece()
|
var portfolio = new SellerProfilePortfolioPiece()
|
||||||
{
|
{
|
||||||
SellerProfileId = existingSellerProfile.Id,
|
SellerProfileId = existingSellerProfile.Id,
|
||||||
|
@ -1,261 +0,0 @@
|
|||||||
using comissions.app.api.Extensions;
|
|
||||||
using comissions.app.api.Models.PortfolioModel;
|
|
||||||
using ArtPlatform.Database;
|
|
||||||
using ArtPlatform.Database.Entities;
|
|
||||||
using comissions.app.api.Models.SellerService;
|
|
||||||
using comissions.app.api.Services.Payment;
|
|
||||||
using comissions.app.api.Services.Storage;
|
|
||||||
using comissions.app.database;
|
|
||||||
using comissions.app.database.Entities;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace comissions.app.api.Controllers;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("api/[controller]")]
|
|
||||||
public class SellerServiceController : Controller
|
|
||||||
{
|
|
||||||
private readonly ApplicationDbContext _dbContext;
|
|
||||||
private readonly IStorageService _storageService;
|
|
||||||
private readonly IPaymentService _paymentService;
|
|
||||||
|
|
||||||
public SellerServiceController(ApplicationDbContext dbContext, IPaymentService paymentService, IStorageService storageService)
|
|
||||||
{
|
|
||||||
_paymentService = paymentService;
|
|
||||||
_storageService = storageService;
|
|
||||||
_dbContext = dbContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[Authorize("read:seller-service")]
|
|
||||||
public async Task<IActionResult> GetSellerServices(int offset=0, int pageSize=10)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var seller = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
|
|
||||||
if(seller==null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if(seller.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var sellerServices = await _dbContext.SellerServices.Where(x=>x.Archived==false).Include(x=>x.Reviews)
|
|
||||||
.Skip(offset).Take(pageSize).ToListAsync();
|
|
||||||
var result = sellerServices.Select(x=>x.ToModel()).ToList();
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[Route("Count")]
|
|
||||||
[Authorize("read:seller-service")]
|
|
||||||
public async Task<IActionResult> GetSellerServicesCount()
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var seller = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
|
|
||||||
if(seller==null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if(seller.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var sellerServices = await _dbContext.SellerServices.Where(x=>x.Archived==false).Include(x => x.Reviews).ToListAsync();
|
|
||||||
var result = sellerServices.Count;
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
[Authorize("write:seller-service")]
|
|
||||||
public async Task<IActionResult> CreateSellerService([FromBody] SellerServiceCreateModel model)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var seller = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
|
|
||||||
if(seller==null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if(seller.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if(seller.StripeAccountId==null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if (_paymentService.SellerAccountIsOnboarded(seller.StripeAccountId) == false)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var sellerService = new SellerService()
|
|
||||||
{
|
|
||||||
Name = model.Name,
|
|
||||||
Description = model.Description,
|
|
||||||
Price = model.Price,
|
|
||||||
SellerProfileId = seller.Id
|
|
||||||
};
|
|
||||||
sellerService = _dbContext.SellerServices.Add(sellerService).Entity;
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
var result = sellerService.ToModel();
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPut]
|
|
||||||
[Authorize("write:seller-service")]
|
|
||||||
[Route("{sellerServiceId:int}")]
|
|
||||||
public async Task<IActionResult> UpdateSellerService([FromBody] SellerServiceUpdateModel model, int sellerServiceId)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var seller = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
|
|
||||||
if(seller==null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if(seller.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var sellerService = await _dbContext.SellerServices.FirstOrDefaultAsync(sellerService=>sellerService.Id==sellerServiceId);
|
|
||||||
|
|
||||||
if(sellerService==null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
sellerService.Name = model.Name;
|
|
||||||
sellerService.Description = model.Description;
|
|
||||||
sellerService.Price = model.Price;
|
|
||||||
|
|
||||||
sellerService = _dbContext.SellerServices.Update(sellerService).Entity;
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
var result = sellerService.ToModel();
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpDelete]
|
|
||||||
[Authorize("write:seller-service")]
|
|
||||||
[Route("{sellerServiceId:int}")]
|
|
||||||
public async Task<IActionResult> DeleteSellerService(int sellerServiceId)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var seller = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
|
|
||||||
if(seller==null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
if(seller.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var sellerService = await _dbContext.SellerServices.FirstOrDefaultAsync(sellerService=>sellerService.Id==sellerServiceId);
|
|
||||||
|
|
||||||
if(sellerService==null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
sellerService.Archived = true;
|
|
||||||
_dbContext.SellerServices.Update(sellerService);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[Route("{sellerServiceId:int}/Portfolio/")]
|
|
||||||
public async Task<IActionResult> GetPortfolio(int sellerServiceId)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
|
|
||||||
|
|
||||||
if (existingSellerProfile == null)
|
|
||||||
{
|
|
||||||
var sellerProfileRequest = await _dbContext.SellerProfileRequests.FirstOrDefaultAsync(request=>request.UserId==userId && request.Accepted==false);
|
|
||||||
if(sellerProfileRequest!=null)
|
|
||||||
return BadRequest();
|
|
||||||
return Unauthorized();
|
|
||||||
}
|
|
||||||
if(existingSellerProfile.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
var portfolio = await _dbContext.SellerProfilePortfolioPieces.Where(x=>x.SellerProfileId==existingSellerProfile.Id && x.SellerServiceId==sellerServiceId).ToListAsync();
|
|
||||||
var result = portfolio.Select(x=>x.ToModel()).ToList();
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[Authorize("read:seller-service")]
|
|
||||||
[Route("{sellerServiceId:int}/Portfolio/{portfolioId:int}")]
|
|
||||||
public async Task<IActionResult> GetPortfolio(int sellerServiceId, int portfolioId)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
if (existingSellerProfile == null)
|
|
||||||
{
|
|
||||||
var sellerProfileRequest = await _dbContext.SellerProfileRequests.FirstOrDefaultAsync(request=>request.UserId==userId && request.Accepted==false);
|
|
||||||
if(sellerProfileRequest!=null)
|
|
||||||
return BadRequest();
|
|
||||||
return Unauthorized();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(existingSellerProfile.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var portfolio = await _dbContext.SellerProfilePortfolioPieces
|
|
||||||
.FirstAsync(x => x.SellerProfileId == existingSellerProfile.Id
|
|
||||||
&& x.SellerServiceId == sellerServiceId && x.Id==portfolioId);
|
|
||||||
var content = await _storageService.DownloadImageAsync(portfolio.FileReference);
|
|
||||||
return new FileStreamResult(content, "application/octet-stream");
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
[Authorize("write:seller-service")]
|
|
||||||
[Route("{sellerServiceId:int}/Portfolio")]
|
|
||||||
public async Task<IActionResult> AddPortfolio(IFormFile file, int sellerServiceId)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
if (existingSellerProfile == null)
|
|
||||||
{
|
|
||||||
var sellerProfileRequest = await _dbContext.SellerProfileRequests.FirstOrDefaultAsync(request=>request.UserId==userId && request.Accepted==false);
|
|
||||||
if(sellerProfileRequest!=null)
|
|
||||||
return BadRequest();
|
|
||||||
return Unauthorized();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(existingSellerProfile.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
var url = await _storageService.UploadImageAsync(file, Guid.NewGuid().ToString());
|
|
||||||
var portfolio = new SellerProfilePortfolioPiece()
|
|
||||||
{
|
|
||||||
SellerProfileId = existingSellerProfile.Id,
|
|
||||||
FileReference = url,
|
|
||||||
SellerServiceId = sellerServiceId
|
|
||||||
};
|
|
||||||
portfolio.SellerProfileId = existingSellerProfile.Id;
|
|
||||||
_dbContext.SellerProfilePortfolioPieces.Add(portfolio);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
var result = portfolio.ToModel();
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpDelete]
|
|
||||||
[Authorize("write:seller-service")]
|
|
||||||
[Route("{sellerServiceId:int}/Portfolio/{portfolioId:int}")]
|
|
||||||
public async Task<IActionResult> DeletePortfolio(int portfolioId)
|
|
||||||
{
|
|
||||||
var userId = User.GetUserId();
|
|
||||||
var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId);
|
|
||||||
if (existingSellerProfile == null)
|
|
||||||
{
|
|
||||||
var sellerProfileRequest = await _dbContext.SellerProfileRequests.FirstOrDefaultAsync(request=>request.UserId==userId && request.Accepted==false);
|
|
||||||
if(sellerProfileRequest!=null)
|
|
||||||
return BadRequest();
|
|
||||||
return Unauthorized();
|
|
||||||
}
|
|
||||||
if(existingSellerProfile.Suspended)
|
|
||||||
return BadRequest();
|
|
||||||
var portfolio = await _dbContext.SellerProfilePortfolioPieces.FirstOrDefaultAsync(x=>x.Id==portfolioId);
|
|
||||||
if(portfolio==null)
|
|
||||||
return NotFound();
|
|
||||||
if(portfolio.SellerProfileId!=existingSellerProfile.Id)
|
|
||||||
return BadRequest();
|
|
||||||
_dbContext.SellerProfilePortfolioPieces.Remove(portfolio);
|
|
||||||
await _dbContext.SaveChangesAsync();
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,6 +2,6 @@ namespace comissions.app.api.Services.Storage;
|
|||||||
|
|
||||||
public interface IStorageService
|
public interface IStorageService
|
||||||
{
|
{
|
||||||
public Task<string> UploadImageAsync(IFormFile file, string fileName);
|
public Task<string> UploadImageAsync(Stream fileStream, string fileName);
|
||||||
public Task<Stream> DownloadImageAsync(string fileRefrence);
|
public Task<Stream> DownloadImageAsync(string fileRefrence);
|
||||||
}
|
}
|
@ -15,12 +15,10 @@ namespace comissions.app.api.Services.Storage
|
|||||||
_client = new HttpClient { BaseAddress = new Uri("https://imgcdn.dev/") };
|
_client = new HttpClient { BaseAddress = new Uri("https://imgcdn.dev/") };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> UploadImageAsync(IFormFile file, string fileName)
|
public async Task<string> UploadImageAsync(Stream fileStream, string fileName)
|
||||||
{
|
{
|
||||||
using var content = new MultipartFormDataContent();
|
using var content = new MultipartFormDataContent();
|
||||||
content.Add(new StringContent(ApiKey), "key");
|
content.Add(new StringContent(ApiKey), "key");
|
||||||
|
|
||||||
using var fileStream = file.OpenReadStream();
|
|
||||||
content.Add(new StreamContent(fileStream), "source", fileName);
|
content.Add(new StreamContent(fileStream), "source", fileName);
|
||||||
|
|
||||||
var response = await _client.PostAsync("api/1/upload", content);
|
var response = await _client.PostAsync("api/1/upload", content);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user