예제 #1
0
        public IActionResult GetPage([PositiveInteger] int requestedPage)
        {
            const int elementsPerPage = 10;

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

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

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

            // Materialización de los registros.
            List <EmployeePersonalInfoDTO> employees = BaseSC
                                                       .MaterializeIQueryable <Employee, EmployeePersonalInfoDTO>(dbEmployees);

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

            return(Ok(response));
        }
        public IActionResult Delete(int id)
        {
            Employee dataBaseEmployee = new EmployeeSC().GetEmployeeById(id);

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

            try
            {
                new EmployeeSC().DeleteEmployee(dataBaseEmployee);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

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

                throw;
            }

            return(NoContent());
        }
예제 #3
0
        public IActionResult Post([FromBody] EmployeePersonalInfoPostDTO newEmployee)
        {
            // Se añade un registro y se obtiene el id con el que se registró.
            int id = new EmployeeSC().AddNewEmployee(newEmployee);

            return(Created("GET " + Request.Path.Value + "/" + id, new { Id = id }));
        }
        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 EmployeeSC().CalculateLastPage(elementsPerPage);
            Pagination <EmployeePersonalInfoDTO> response = new(requestedPage, lastPage);

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

            // Get Selected Page
            IQueryable <Employee>          dbEmployees = new EmployeeSC().GetPage(elementsPerPage, (int)response.CurrentPage);
            List <EmployeePersonalInfoDTO> employees   = BaseSC.MaterializeIQueryable <Employee, EmployeePersonalInfoDTO>(dbEmployees);

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

            return(Ok(response));
        }
        public IActionResult Put(int id, [FromBody] EmployePersonalInfoPutDTO modifiedEmployee)
        {
            Employee dataBaseEmployee = new EmployeeSC().GetEmployeeById(id);

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

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

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

                throw;
            }

            return(NoContent());
        }
        public IActionResult GetAll()
        {
            IQueryable <Employee> dbEmployees = new EmployeeSC().GetAllEmployees();

            List <EmployeePersonalInfoDTO> employees = BaseSC.MaterializeIQueryable <Employee, EmployeePersonalInfoDTO>(dbEmployees);

            return(Ok(employees));
        }
예제 #7
0
        public IActionResult Get([PositiveInteger] int id)
        {
            // Obtiene el empleado de la Base de Datos.
            Employee dbEmployee = new EmployeeSC().GetEmployeeById(id);
            // Genera un modelo con la información del empleado obtenido.
            EmployeePersonalInfoDTO employee = new(dbEmployee);

            return(Ok(employee));
        }
예제 #8
0
        public IActionResult GetAll()
        {
            // Se obtienen todos los registros.
            IQueryable <Employee> dbEmployees = new EmployeeSC().GetAllEmployees();

            // Se materialzian.
            List <EmployeePersonalInfoDTO> employees = BaseSC
                                                       .MaterializeIQueryable <Employee, EmployeePersonalInfoDTO>(dbEmployees);

            return(Ok(employees));
        }
예제 #9
0
        public List <EmployeeModel> Get()
        {
            var employees = new EmployeeSC().GetAllEmployees().Select(s => new EmployeeModel
            {
                IdNumber   = s.EmployeeId,
                Name       = s.FirstName,
                FamilyName = s.LastName
            }).ToList();

            return(employees);
        }
        public IActionResult Put(int id, [FromBody] EmployeePersonalInfoDTO modifiedEmployee)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    EmployeeSC.UpdateEmployee(dbContext, id, modifiedEmployee);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IActionResult Delete(int id)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    EmployeeSC.DeleteEmployee(dbContext, id);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IActionResult Get()
        {
            List <EmployeePersonalInfoDTO> employees = new();

            using (NorthwindContext dbContext = new())
            {
                IQueryable <Employee> dbEmployees = EmployeeSC.GetAllEmployees(dbContext).AsNoTracking();

                foreach (Employee dbEmployee in dbEmployees)
                {
                    employees.Add(new EmployeePersonalInfoDTO(dbEmployee));
                }
                ;
            }

            return(Ok(employees));
        }
        public IActionResult Post([FromBody] EmployeePersonalInfoDTO newEmployee)
        {
            int id;

            try
            {
                using (NorthwindContext dbContext = new())
                {
                    id = EmployeeSC.AddNewEmployee(dbContext, newEmployee);
                }

                return(Ok("Registered Id: " + id));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
예제 #14
0
        public EmployeeModel Get(int id)
        {
            var employee      = new EmployeeSC().GetEmployeeById(id);
            var employeeModel = new EmployeeModel();

            try
            {
                employeeModel.IdNumber   = employee.EmployeeId;
                employeeModel.Name       = employee.FirstName;
                employeeModel.FamilyName = employee.LastName;
            }
            catch
            {
                employeeModel.IdNumber   = null;
                employeeModel.Name       = "";
                employeeModel.FamilyName = "";
            }
            return(employeeModel);
        }
        public IActionResult Get(int id)
        {
            if (id < 1)
            {
                return(BadRequest($"{nameof(id)} must be at least 1."));
            }

            // Get Employee from Database
            Employee dbEmployee = new EmployeeSC().GetEmployeeById(id);

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

            EmployeePersonalInfoDTO employee = new(dbEmployee);

            return(Ok(employee));
        }
예제 #16
0
        public EmployeeModel Get(int id)
        {
            var employee      = new EmployeeSC().GetEmployeeById(id);
            var employeeModel = new EmployeeModel();

            try // Como el metodo GetEmployeerById devuelve un null si no lo encuentra se puso en un try catch
            {   // ya que el EmployeeModel no acepta datos nulos
                employeeModel.IdNumber   = employee.EmployeeId;
                employeeModel.Name       = employee.FirstName;
                employeeModel.FamilyName = employee.LastName;
            }
            catch
            {
                employeeModel.IdNumber   = null;
                employeeModel.Name       = "";
                employeeModel.FamilyName = "";
            }
            return(employeeModel);
        }
        public IActionResult Get(int id)
        {
            try
            {
                EmployeePersonalInfoDTO employee;

                using (NorthwindContext dbContext = new())
                {
                    Employee dbEmployee = EmployeeSC.GetEmployeeById(dbContext, id);

                    employee = new(dbEmployee);
                }

                return(Ok(employee));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public IActionResult Post([FromBody] EmployePersonalInfoPostDTO newEmployee)
        {
            int id;

            try
            {
                id = new EmployeeSC().AddNewEmployee(newEmployee);
            }
            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 List <Employee> Get()
        {
            var employees = new EmployeeSC().GetAllEmployees().ToList();

            return(employees);
        }
예제 #20
0
        public void Post([FromBody] EmployeeModel newEmployee)
        {
            var employee = new EmployeeSC();

            employee.AddEmployee(newEmployee);
        }
예제 #21
0
        public static void Ejercicio()
        {
            var output = new EmployeeSC().GetEmployees();

            Console.WriteLine(output);
        }