back photo + event tags and persons
This commit is contained in:
@@ -1,112 +1,98 @@
|
||||
using back.ApiService.context;
|
||||
using back.ApiService.models;
|
||||
using back.context;
|
||||
using back.DataModels;
|
||||
using back.DTO;
|
||||
using back.persistance.blob;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace back.controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PhotosController(PhotoContext photoContext) : ControllerBase
|
||||
public class PhotosController(PhotoContext photoContext, IBlobStorageService blobStorage) : ControllerBase
|
||||
{
|
||||
private readonly PhotoContext _photoContext = photoContext;
|
||||
|
||||
// GET: api/<PhotoController>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<Photo>>> Get([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
||||
public async Task<ActionResult<IEnumerable<PhotoModel>>> Get([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
||||
{
|
||||
if (page < 1) page = 1;
|
||||
if (pageSize < 1) pageSize = 20;
|
||||
|
||||
var totalItems = await _photoContext.Photos.CountAsync();
|
||||
var photos = await _photoContext.Photos
|
||||
.OrderByDescending(p => p.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
var photos = await _photoContext.GetPage(page, pageSize);
|
||||
var totalItems = await _photoContext.GetTotalItems();
|
||||
|
||||
Response.Headers.Append("X-Total-Count", totalItems.ToString());
|
||||
|
||||
return Ok(photos);
|
||||
}
|
||||
|
||||
// GET api/<PhotoController>/5
|
||||
[HttpGet("{id}/{res}")]
|
||||
public async Task<IActionResult> Get(Guid id, string res = "low")
|
||||
[HttpGet("{res}/{id}")]
|
||||
public async Task<IActionResult> Get(string res, Guid id)
|
||||
{
|
||||
var photo = await _photoContext.Photos.FindAsync(id);
|
||||
var photo = await _photoContext.GetById(id);
|
||||
if (photo == null)
|
||||
return NotFound();
|
||||
|
||||
string? filePath = res.ToLower() switch
|
||||
{
|
||||
"low" => photo.LowResUrl,
|
||||
"mid" => photo.MidResUrl,
|
||||
"high" => photo.HighResUrl,
|
||||
_ => null
|
||||
"mid" => photo.MidResUrl,
|
||||
"low" or _ => photo.LowResUrl
|
||||
};
|
||||
|
||||
if (filePath == null || !System.IO.File.Exists(Path.Combine(Constants.Data, filePath)))
|
||||
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();
|
||||
}
|
||||
|
||||
var fileBytes = await System.IO.File.ReadAllBytesAsync(Path.Combine(Constants.Data, filePath));
|
||||
var contentType = "image/jpeg"; // Cambia si usas otro formato
|
||||
|
||||
return File(fileBytes, contentType);
|
||||
return File(file, mediaType);
|
||||
}
|
||||
|
||||
// POST api/<PhotoController>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromForm] PhotoFormModel form)
|
||||
{
|
||||
try
|
||||
try
|
||||
{
|
||||
if (form.Image == null || form.Image.Length == 0)
|
||||
return BadRequest("No image uploaded.");
|
||||
|
||||
var photo = PhotoBuilder.Build(
|
||||
form.Title,
|
||||
form.Description,
|
||||
form.Tags?.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList(),
|
||||
form.People?.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList(),
|
||||
form.Image
|
||||
);
|
||||
|
||||
// Guarda la información en la base de datos
|
||||
_photoContext.Photos.Add(photo);
|
||||
await _photoContext.SaveChangesAsync();
|
||||
await _photoContext.CreateNew(form);
|
||||
|
||||
return Created();
|
||||
}
|
||||
catch
|
||||
catch
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
//// PUT api/<PhotoController>/5
|
||||
//[HttpPut("{id}")]
|
||||
//public async Task<IActionResult> Put(Guid id, [FromBody] Photo photo)
|
||||
//{
|
||||
// if (id != photo.Id)
|
||||
// return BadRequest();
|
||||
|
||||
// _photoContext.Entry(photo).State = EntityState.Modified;
|
||||
// await _photoContext.SaveChangesAsync();
|
||||
// return NoContent();
|
||||
//}
|
||||
//// PUT api/<PhotoController>
|
||||
[HttpPut]
|
||||
public async Task<IActionResult> Put([FromBody] PhotoModel photo)
|
||||
{
|
||||
await _photoContext.Update(photo);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// DELETE api/<PhotoController>/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete(Guid id)
|
||||
{
|
||||
var photo = await _photoContext.Photos.FindAsync(id);
|
||||
var photo = await _photoContext.GetById(id);
|
||||
if (photo == null)
|
||||
return NotFound();
|
||||
|
||||
_photoContext.Photos.Remove(photo);
|
||||
await _photoContext.SaveChangesAsync();
|
||||
await _photoContext.Delete(photo);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user