コード例 #1
0
        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());
        }
コード例 #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));
        }
コード例 #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 }));
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        public IActionResult GetAll()
        {
            IQueryable <Customer> dbCustomers = new CustomerSC().GetAllCustomers();

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

            return(Ok(customers));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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));
        }
コード例 #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));
            }
        }
コード例 #13
0
        public IActionResult Delete(string id)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    CustomerSC.DeleteCustomer(dbContext, id);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
コード例 #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));
        }
コード例 #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));
            }
        }
コード例 #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));
            }
        }
コード例 #17
0
        // 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);
        }
コード例 #18
0
        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));
        }
コード例 #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);
        }