transactions
This commit is contained in:
@@ -1,60 +1,33 @@
|
||||
using back.context;
|
||||
using back.DataModels;
|
||||
using back.DataModels;
|
||||
using back.DTO;
|
||||
using back.persistance.blob;
|
||||
using back.services.bussines.PhotoService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace back.controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PhotosController(PhotoContext photoContext, IBlobStorageService blobStorage) : ControllerBase
|
||||
public class PhotosController(IPhotoService photoService) : ControllerBase
|
||||
{
|
||||
private readonly PhotoContext _photoContext = photoContext;
|
||||
private readonly IPhotoService _photoService = photoService;
|
||||
|
||||
// GET: api/<PhotoController>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<PhotoModel>>> Get([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
||||
public async Task<ActionResult<IEnumerable<Photo>>> Get([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
||||
{
|
||||
var photos = await _photoContext.GetPage(page, pageSize);
|
||||
var totalItems = await _photoContext.GetTotalItems();
|
||||
|
||||
(int totalItems, IEnumerable<Photo>? pageData) = await _photoService.GetPage(page, pageSize);
|
||||
Response.Headers.Append("X-Total-Count", totalItems.ToString());
|
||||
return Ok(photos);
|
||||
return Ok(pageData);
|
||||
}
|
||||
|
||||
// GET api/<PhotoController>/5
|
||||
[HttpGet("{res}/{id}")]
|
||||
public async Task<IActionResult> Get(string res, Guid id)
|
||||
[HttpGet("{id}/{res}")]
|
||||
public async Task<IActionResult> Get(string id, string res)
|
||||
{
|
||||
var photo = await _photoContext.GetById(id);
|
||||
if (photo == null)
|
||||
(string? mediaType, byte[]? fileBytes) = await _photoService.GetBytes(id, res.ToLower());
|
||||
if(fileBytes == null)
|
||||
return NotFound();
|
||||
|
||||
string? filePath = res.ToLower() switch
|
||||
{
|
||||
"high" => photo.HighResUrl,
|
||||
"mid" => photo.MidResUrl,
|
||||
"low" or _ => photo.LowResUrl
|
||||
};
|
||||
|
||||
string? mediaType = res.ToLower() switch
|
||||
{
|
||||
"high" => $"image/{photo.Extension}",
|
||||
"mid" or "low" or _ => "image/webp",
|
||||
};
|
||||
|
||||
if (filePath == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
var file = await blobStorage.GetBytesAsync(filePath);
|
||||
if (file == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return File(file, mediaType);
|
||||
return File(fileBytes, mediaType ?? "image/jpeg");
|
||||
}
|
||||
|
||||
// POST api/<PhotoController>
|
||||
@@ -66,7 +39,7 @@ public class PhotosController(PhotoContext photoContext, IBlobStorageService blo
|
||||
if (form.Image == null || form.Image.Length == 0)
|
||||
return BadRequest("No image uploaded.");
|
||||
|
||||
await _photoContext.CreateNew(form);
|
||||
await _photoService.Create(form);
|
||||
|
||||
return Created();
|
||||
}
|
||||
@@ -78,21 +51,17 @@ public class PhotosController(PhotoContext photoContext, IBlobStorageService blo
|
||||
|
||||
//// PUT api/<PhotoController>
|
||||
[HttpPut]
|
||||
public async Task<IActionResult> Put([FromBody] PhotoModel photo)
|
||||
public async Task<IActionResult> Put([FromBody] Photo photo)
|
||||
{
|
||||
await _photoContext.Update(photo);
|
||||
await _photoService.Update(photo);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// DELETE api/<PhotoController>/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete(Guid id)
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
var photo = await _photoContext.GetById(id);
|
||||
if (photo == null)
|
||||
return NotFound();
|
||||
|
||||
await _photoContext.Delete(photo);
|
||||
await _photoService.Delete(id);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user