public async Task <ActionResult <CustomerDto> > GetCustomersByID(int id) { try { _orm.OpenConn(); var customerDB = await _orm.GetCustomerById(id); var customer = _mapper.Map <CustomerDto>(customerDB); if (customerDB == null) { return(NotFound()); } //Map customer addresses to customer var addressListDB = await _orm.GetCustomerAddressesByCustomerID(id); var addressList = _mapper.Map <List <Customer_AddressesDto> >(addressListDB); customer.Addresses = addressList; await _orm.CloseConn(); return(Ok(customer)); } catch (Exception e) { return(StatusCode(500, e.GetType().Name + ", " + e.Message)); } }
public async Task <ActionResult <ProducentDto> > GetProducentByID(int producentID) { _orm.OpenConn(); var producent = await _orm.GetProducentById(producentID); if (producent == null) { return(NotFound()); } var producentDto = _mapper.Map <ProducentDto>(producent); await _orm.CloseConn(); return(Ok(producentDto)); }
public async Task <ActionResult <List <CategoryDto> > > getCategories() { _orm.OpenConn(); var categoriesFromDB = await _orm.GetAllCategories(); if (categoriesFromDB == null) { return(NotFound()); } var categoriesDto = _mapper.Map <List <CategoryDto> >(categoriesFromDB); await _orm.CloseConn(); return(Ok(categoriesDto)); }
public async Task <ActionResult <List <ProductDto> > > GetProducts([FromQuery] ProductsParameter productsParameter) { _orm.OpenConn(); var productsFromDB = await _orm.GetAllProducts(productsParameter); if (productsFromDB == null) { return(NotFound()); } var productsDto = _mapper.Map <List <ProductDto> >(productsFromDB); await _orm.CloseConn(); return(Ok(productsDto)); }
public async Task <ActionResult <OrderDto> > GetOrder(int customerID, int orderID) { //Allow only admins to access other users records var userid = int.Parse(User.Identity.Name); if (userid != customerID && !User.IsInRole(Role.Admin)) { return(Forbid()); } _orm.OpenConn(); var orderFromDB = await _orm.GetOrderById(orderID); if (orderFromDB == null || orderFromDB.Customer.Id != customerID) { return(NotFound()); } var orderDto = _mapper.Map <OrderDto>(orderFromDB); await _orm.CloseConn(); return(Ok(orderDto)); }