-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathItemsController.cs
More file actions
46 lines (39 loc) · 1.36 KB
/
Copy pathItemsController.cs
File metadata and controls
46 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using ECommerce.API.Interfaces.Services;
using ECommerce.API.Models;
using ECommerce.Shared.Models;
using Microsoft.AspNetCore.Mvc;
namespace ECommerce.API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ItemsController(IItemService itemService) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> PostItem([FromBody] CreateItemDto itemDto)
{
await itemService.PostItemAsync(itemDto);
return Created();
}
[HttpGet]
public async Task<ActionResult<PagedResponse<Item>>> GetItemsAsync([FromQuery] PaginationParams paginationParams)
{
var pagedResponse = await itemService.GetItemsAsync(paginationParams);
if (pagedResponse.TotalRecords == 0) return NotFound();
return Ok(pagedResponse);
}
[HttpGet("{id:int}")]
public async Task<ActionResult<ItemDto>> GetItemByIdAsync(int id)
{
var itemDto = await itemService.GetItemByIdAsync(id);
return itemDto is null ? NotFound() : Ok(itemDto);
}
[HttpDelete("{id:int}")]
public async Task<IActionResult> DeleteItemAsync(int id)
{
var success = await itemService.DeleteItemByIdAsync(id);
if (success is null)
return NotFound();
if (success is false)
return StatusCode(StatusCodes.Status500InternalServerError);
return NoContent();
}
}