|
| 1 | +using api.Data; |
| 2 | +using api.Models; |
| 3 | +using api.Repositories; |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | + |
| 8 | +namespace api.Controllers |
| 9 | +{ |
| 10 | + [Route("api/[controller]")] |
| 11 | + [ApiController] |
| 12 | + public class ImageController : ControllerBase |
| 13 | + { |
| 14 | + private readonly IImageRepository _repository; |
| 15 | + private readonly AuthDbContext _context; |
| 16 | + |
| 17 | + public ImageController(IImageRepository repository, AuthDbContext context) |
| 18 | + { |
| 19 | + _repository = repository; |
| 20 | + _context = context; |
| 21 | + } |
| 22 | + |
| 23 | + [HttpPost] |
| 24 | + public async Task<IActionResult> Upload([FromForm] IFormFile file, [FromForm] string name, [FromForm] string category) |
| 25 | + { |
| 26 | + if (file == null || file.Length == 0) |
| 27 | + { |
| 28 | + return BadRequest("No file uploaded."); |
| 29 | + } |
| 30 | + |
| 31 | + using var memoryStream = new MemoryStream(); |
| 32 | + await file.CopyToAsync(memoryStream); |
| 33 | + |
| 34 | + var image = new Image |
| 35 | + { |
| 36 | + Name = name, |
| 37 | + Category = category, |
| 38 | + ContentType = file.ContentType, |
| 39 | + Data = memoryStream.ToArray() |
| 40 | + }; |
| 41 | + |
| 42 | + var createdImage = await _repository.CreateAsync(image); |
| 43 | + return CreatedAtAction(nameof(GetById), new { id = createdImage.Id }, createdImage); |
| 44 | + } |
| 45 | + |
| 46 | + [HttpGet("{id}")] |
| 47 | + public async Task<IActionResult> GetById(Guid id) |
| 48 | + { |
| 49 | + var image = await _repository.GetByIdAsync(id); |
| 50 | + if (image == null) |
| 51 | + { |
| 52 | + return NotFound(); |
| 53 | + } |
| 54 | + |
| 55 | + return File(image.Data, image.ContentType); |
| 56 | + } |
| 57 | + |
| 58 | + [HttpGet] |
| 59 | + public async Task<IActionResult> GetAll([FromQuery] int page = 1, [FromQuery] int pageSize = 10) |
| 60 | + { |
| 61 | + if (page < 1 || pageSize < 1) |
| 62 | + { |
| 63 | + return BadRequest("Page and pageSize must be greater than 0."); |
| 64 | + } |
| 65 | + |
| 66 | + var pagedImages = await _repository.GetAllAsync(page, pageSize); |
| 67 | + |
| 68 | + var response = new PageResponse<object> |
| 69 | + { |
| 70 | + Items = pagedImages.Items.Select(i => new |
| 71 | + { |
| 72 | + i.Id, |
| 73 | + i.Name, |
| 74 | + i.Category, |
| 75 | + i.ContentType, |
| 76 | + i.CreatedAt, |
| 77 | + i.UpdatedAt, |
| 78 | + Base64 = $"data:{i.ContentType};base64,{Convert.ToBase64String(i.Data)}" |
| 79 | + }), |
| 80 | + Page = pagedImages.Page, |
| 81 | + PageSize = pagedImages.PageSize, |
| 82 | + TotalCount = pagedImages.TotalCount |
| 83 | + }; |
| 84 | + |
| 85 | + return Ok(response); |
| 86 | + } |
| 87 | + |
| 88 | + [HttpGet("category/{category}")] |
| 89 | + public async Task<IActionResult> GetByCategory(string category, [FromQuery] int page = 1, [FromQuery] int pageSize = 10) |
| 90 | + { |
| 91 | + if (page < 1 || pageSize < 1) |
| 92 | + { |
| 93 | + return BadRequest("Page and pageSize must be greater than 0."); |
| 94 | + } |
| 95 | + |
| 96 | + var pagedImages = await _repository.GetByCategoryAsync(category, page, pageSize); |
| 97 | + var response = new PageResponse<object> |
| 98 | + { |
| 99 | + Items = pagedImages.Items.Select(i => new { i.Id, i.Name, i.Category, i.ContentType, i.CreatedAt, i.UpdatedAt }), |
| 100 | + Page = pagedImages.Page, |
| 101 | + PageSize = pagedImages.PageSize, |
| 102 | + TotalCount = pagedImages.TotalCount |
| 103 | + }; |
| 104 | + |
| 105 | + return Ok(response); |
| 106 | + } |
| 107 | + |
| 108 | + [HttpPut("{id}")] |
| 109 | + public async Task<IActionResult> Update(Guid id, [FromForm] IFormFile file, [FromForm] string name, [FromForm] string category) |
| 110 | + { |
| 111 | + if (file == null || file.Length == 0) |
| 112 | + { |
| 113 | + return BadRequest("No file uploaded."); |
| 114 | + } |
| 115 | + |
| 116 | + using var memoryStream = new MemoryStream(); |
| 117 | + await file.CopyToAsync(memoryStream); |
| 118 | + |
| 119 | + var image = new Image |
| 120 | + { |
| 121 | + Id = id, |
| 122 | + Name = name, |
| 123 | + Category = category, |
| 124 | + ContentType = file.ContentType, |
| 125 | + Data = memoryStream.ToArray() |
| 126 | + }; |
| 127 | + |
| 128 | + var updatedImage = await _repository.UpdateAsync(image); |
| 129 | + if (updatedImage == null) |
| 130 | + { |
| 131 | + return NotFound(); |
| 132 | + } |
| 133 | + |
| 134 | + return Ok(updatedImage); |
| 135 | + } |
| 136 | + |
| 137 | + [HttpDelete("{id}")] |
| 138 | + public async Task<IActionResult> Delete(Guid id) |
| 139 | + { |
| 140 | + var deleted = await _repository.DeleteAsync(id); |
| 141 | + if (!deleted) |
| 142 | + { |
| 143 | + return NotFound(); |
| 144 | + } |
| 145 | + |
| 146 | + return NoContent(); |
| 147 | + } |
| 148 | + |
| 149 | + [HttpGet("categories")] |
| 150 | + public async Task<IActionResult> GetAllCategory() |
| 151 | + { |
| 152 | + var categories = await _context.Images |
| 153 | + .Select(img => img.Category) |
| 154 | + .Distinct() |
| 155 | + .ToListAsync(); |
| 156 | + |
| 157 | + return Ok(categories); |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | +} |
0 commit comments