From 8f50f3b6c346af20aa7ca6e081472aa6abf34188 Mon Sep 17 00:00:00 2001 From: Damien Ostler Date: Thu, 15 Feb 2024 01:07:43 -0500 Subject: [PATCH] feat: removed service controller --- .../Controllers/SellerProfileController.cs | 4 +- .../Controllers/SellerServiceController.cs | 261 ------------------ .../Services/Storage/IStorageService.cs | 2 +- .../Storage/ImgCdnStorageServiceProvider.cs | 4 +- 4 files changed, 4 insertions(+), 267 deletions(-) delete mode 100644 src/comissions.app.api/Controllers/SellerServiceController.cs diff --git a/src/comissions.app.api/Controllers/SellerProfileController.cs b/src/comissions.app.api/Controllers/SellerProfileController.cs index ee95e61..bd375a9 100644 --- a/src/comissions.app.api/Controllers/SellerProfileController.cs +++ b/src/comissions.app.api/Controllers/SellerProfileController.cs @@ -154,7 +154,7 @@ public class SellerProfileController : Controller [HttpPost] [Route("Portfolio")] [Authorize("write:seller-profile")] - public async Task AddPortfolio([FromBody]IFormFile newImage) + public async Task AddPortfolio() { var userId = User.GetUserId(); var existingSellerProfile = await _dbContext.UserSellerProfiles.FirstOrDefaultAsync(sellerProfile=>sellerProfile.UserId==userId); @@ -165,7 +165,7 @@ public class SellerProfileController : Controller if(existingSellerProfile.Suspended) 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() { SellerProfileId = existingSellerProfile.Id, diff --git a/src/comissions.app.api/Controllers/SellerServiceController.cs b/src/comissions.app.api/Controllers/SellerServiceController.cs deleted file mode 100644 index 3b04947..0000000 --- a/src/comissions.app.api/Controllers/SellerServiceController.cs +++ /dev/null @@ -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 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 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 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 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 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 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 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 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 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(); - } -} - diff --git a/src/comissions.app.api/Services/Storage/IStorageService.cs b/src/comissions.app.api/Services/Storage/IStorageService.cs index 905e4b6..3fa41db 100644 --- a/src/comissions.app.api/Services/Storage/IStorageService.cs +++ b/src/comissions.app.api/Services/Storage/IStorageService.cs @@ -2,6 +2,6 @@ namespace comissions.app.api.Services.Storage; public interface IStorageService { - public Task UploadImageAsync(IFormFile file, string fileName); + public Task UploadImageAsync(Stream fileStream, string fileName); public Task DownloadImageAsync(string fileRefrence); } \ No newline at end of file diff --git a/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs b/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs index b92ddac..35a6213 100644 --- a/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs +++ b/src/comissions.app.api/Services/Storage/ImgCdnStorageServiceProvider.cs @@ -15,12 +15,10 @@ namespace comissions.app.api.Services.Storage _client = new HttpClient { BaseAddress = new Uri("https://imgcdn.dev/") }; } - public async Task UploadImageAsync(IFormFile file, string fileName) + public async Task UploadImageAsync(Stream fileStream, string fileName) { using var content = new MultipartFormDataContent(); content.Add(new StringContent(ApiKey), "key"); - - using var fileStream = file.OpenReadStream(); content.Add(new StreamContent(fileStream), "source", fileName); var response = await _client.PostAsync("api/1/upload", content);