public IActionResult Delete(string id)
        {
            Customer dataBaseCustomer = new CustomerSC().GetCustomerById(id);

            if (dataBaseCustomer == null)
            {
                return(NotFound());
            }

            try
            {
                new CustomerSC().DeleteCustomer(dataBaseCustomer);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

                if (message != null)
                {
                    return(Conflict(message));
                }

                throw;
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public IActionResult GetPage([PositiveInteger] int requestedPage)
        {
            const int elementsPerPage = 10;

            // Cálculo de las Páginas.
            int lastPage = new CustomerSC().CalculateLastPage(elementsPerPage);
            Pagination <CustomerContactInfoDTO> response = new(requestedPage, lastPage);

            if (lastPage == 0)
            {
                return(Ok(response));
            }

            // Selección de la página solicitada.
            IQueryable <Customer> dbCustomers = new CustomerSC()
                                                .GetPage(elementsPerPage, (int)response.CurrentPage);

            // Materialización de los registros.
            List <CustomerContactInfoDTO> customers = BaseSC
                                                      .MaterializeIQueryable <Customer, CustomerContactInfoDTO>(dbCustomers);

            // Los registros se añaden a la respuesta.
            response.ResponseList = customers;

            return(Ok(response));
        }
Exemplo n.º 3
0
        public IActionResult Post([FromBody] CustomerContactInfoPostDTO newCustomer)
        {
            // Se añade un registro y se obtiene el id con el que se registró.
            string id = new CustomerSC().AddNewCustomer(newCustomer);

            return(Created("GET " + Request.Path.Value + "/" + id, new { Id = id }));
        }
        public IActionResult Put(string id, [FromBody] CustomerContactInfoPutDTO modifiedCustomer)
        {
            Customer dataBaseCustomer = new CustomerSC().GetCustomerById(id);

            if (dataBaseCustomer == null)
            {
                return(NotFound());
            }

            try
            {
                //TODO: Check if it is possible to pass the dataBaseCustomer insted of the id.
                new CustomerSC().UpdateCustomer(id, modifiedCustomer);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

                if (message != null)
                {
                    return(Conflict(message));
                }

                throw;
            }

            return(NoContent());
        }
        public IActionResult GetPage(int requestedPage)
        {
            const int elementsPerPage = 10;

            if (requestedPage < 1)
            {
                return(BadRequest($"{nameof(requestedPage)} must be at least 1."));
            }

            // Calculate Pages
            int lastPage = new CustomerSC().CalculateLastPage(elementsPerPage);
            Pagination <CustomerContactInfoDTO> response = new(requestedPage, lastPage);

            if (lastPage == 0)
            {
                return(Ok(response));
            }

            // Get Selected Page
            IQueryable <Customer>         dbCustomers = new CustomerSC().GetPage(elementsPerPage, (int)response.CurrentPage);
            List <CustomerContactInfoDTO> customers   = BaseSC.MaterializeIQueryable <Customer, CustomerContactInfoDTO>(dbCustomers);

            // Attach elements of the page to the response
            response.ResponseList = customers;

            return(Ok(response));
        }
        public IActionResult GetAll()
        {
            IQueryable <Customer> dbCustomers = new CustomerSC().GetAllCustomers();

            List <CustomerContactInfoDTO> customers = BaseSC.MaterializeIQueryable <Customer, CustomerContactInfoDTO>(dbCustomers);

            return(Ok(customers));
        }
Exemplo n.º 7
0
        public IActionResult Get(string id)
        {
            // Obtiene el empleado de la Base de Datos.
            Customer dbCustomer = new CustomerSC().GetCustomerById(id);
            // Genera un modelo con la información del empleado obtenido.
            CustomerContactInfoDTO customer = new(dbCustomer);

            return(Ok(customer));
        }
Exemplo n.º 8
0
        public IActionResult GetAll()
        {
            // Se obtienen todos los registros.
            IQueryable <Customer> dbCustomers = new CustomerSC().GetAllCustomers();

            // Se materialzian.
            List <CustomerContactInfoDTO> customers = BaseSC
                                                      .MaterializeIQueryable <Customer, CustomerContactInfoDTO>(dbCustomers);

            return(Ok(customers));
        }
Exemplo n.º 9
0
        public List <CustomerModel> Get()
        {
            var customers = new CustomerSC().GetAllCustomers().Select(s => new CustomerModel
            {
                idString    = s.CustomerId,
                name        = s.CompanyName,
                cityName    = s.City,
                phoneNumber = s.Phone
            }).ToList();

            return(customers);
        }
        public List <CustomerModel> Get() // Obtiene todos los clientes en una lista
        {
            var customers = new CustomerSC().GetAllCustomers().Select(s => new CustomerModel
            {
                // Por seguridad pasa los datos a otro modelo, para que no tengan los nombres de los atributos de la db
                idString    = s.CustomerId,
                name        = s.CompanyName,
                cityName    = s.City,
                phoneNumber = s.Phone
            }).ToList();

            return(customers);
        }
        public IActionResult Get(string id)
        {
            // Get Customer from Database
            Customer dbCustomer = new CustomerSC().GetCustomerById(id);

            if (dbCustomer == null)
            {
                return(NotFound());
            }

            CustomerContactInfoDTO customer = new(dbCustomer);

            return(Ok(customer));
        }
Exemplo n.º 12
0
        public IActionResult Put(string id, [FromBody] CustomerContactInfoDTO modifiedCustomer)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    CustomerSC.UpdateCustomer(dbContext, id, modifiedCustomer);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 13
0
        public IActionResult Delete(string id)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    CustomerSC.DeleteCustomer(dbContext, id);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 14
0
        public IActionResult Get()
        {
            List <CustomerContactInfoDTO> customers = new();

            using (NorthwindContext dbContext = new())
            {
                IQueryable <Customer> dbCustomers = CustomerSC.GetAllCustomers(dbContext).AsNoTracking();

                foreach (Customer dbCustomer in dbCustomers)
                {
                    customers.Add(new CustomerContactInfoDTO(dbCustomer));
                }
                ;
            }

            return(Ok(customers));
        }
Exemplo n.º 15
0
        public IActionResult Post([FromBody] CustomerContactInfoDTO newCustomer)
        {
            string id;

            try
            {
                using (NorthwindContext dbContext = new())
                {
                    id = CustomerSC.AddNewCustomer(dbContext, newCustomer);
                }

                return(Ok("Registered Id: " + id));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 16
0
        public IActionResult Get(string id)
        {
            try
            {
                CustomerContactInfoDTO customer;

                using (NorthwindContext dbContext = new())
                {
                    Customer dbCustomer = CustomerSC.GetCustomerById(dbContext, id);

                    customer = new(dbCustomer);
                }

                return(Ok(customer));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        // Obtiene un cliente por su id
        public CustomerModel Get(string id)
        {
            var customer      = new CustomerSC().GetCustomerById(id);
            var customerModel = new CustomerModel();

            try // Como el metodo GetCustomerById devuelve un null si no lo encuentra se puso en un try catch
            {   // ya que el customerModel no acepta datos nulos
                customerModel.idString    = customer.CustomerId;
                customerModel.name        = customer.CompanyName;
                customerModel.cityName    = customer.City;
                customerModel.phoneNumber = customer.Phone;
            }
            catch
            {
                customerModel.idString    = "";
                customerModel.name        = "";
                customerModel.cityName    = "";
                customerModel.phoneNumber = "";
            }

            return(customerModel);
        }
        public IActionResult Post([FromBody] CustomerContactInfoPostDTO newCustomer)
        {
            string id;

            try
            {
                id = new CustomerSC().AddNewCustomer(newCustomer);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

                if (message != null)
                {
                    return(Conflict(message));
                }

                throw;
            }

            return(Created("GET " + Request.Path.Value + "/" + id, id));
        }
Exemplo n.º 19
0
        public CustomerModel Get(string id)
        {
            var customer      = new CustomerSC().GetCustomerById(id);
            var customerModel = new CustomerModel();

            try
            {
                customerModel.idString    = customer.CustomerId;
                customerModel.name        = customer.CompanyName;
                customerModel.cityName    = customer.City;
                customerModel.phoneNumber = customer.Phone;
            }
            catch
            {
                customerModel.idString    = "";
                customerModel.name        = "";
                customerModel.cityName    = "";
                customerModel.phoneNumber = "";
            }

            return(customerModel);
        }