-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductController.cs
More file actions
48 lines (42 loc) · 1.57 KB
/
ProductController.cs
File metadata and controls
48 lines (42 loc) · 1.57 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
47
48
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Repositories;
using Services;
using System.Collections.Generic;
namespace LoginProject.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class productController : ControllerBase
{
private IProductService _productService;
private IMapper _mapper;
public productController(IProductService productService, IMapper mapper)
{
_productService = productService;
_mapper = mapper;
}
[HttpGet]
public async Task<ActionResult<List<ProductDto>>> GetProducts([FromQuery]int position, [FromQuery] int skip, [FromQuery] string? desc, [FromQuery] int? minPrice
, [FromQuery] int? maxPrice, [FromQuery] int?[] categoryIds)
{
List<Product> products = await _productService.getProducts(position, skip, desc, minPrice,maxPrice, categoryIds);
List<ProductDto> productsDto = _mapper.Map<List<Product>, List<ProductDto>>(products);
if (productsDto != null)
{
return Ok(productsDto);
}
return NotFound();
}
// GET api/<ProductController>/5
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetById(int id)
{
Product product = await _productService.getProductById(id);
ProductDto productDto = _mapper.Map<Product, ProductDto>(product);
if (productDto == null)
return NotFound();
return Ok(productDto);
}
}
}