public HttpResponseMessage Get(string firstName)
        {
            try
            {
                // The commented code moved to EmployeeDataAccess.cs
                //connection.Open();

                //var result =
                //    connection.Query<EmployeeListResult>("Select * From Employee where FirstName = @firstname",
                //        new { firstName }).FirstOrDefault();]

                var employeeData = new EmployeeDataAccess();
                var result       = employeeData.Get(firstName);

                if (result == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Employee with the Name {firstName} was not found"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Example #2
0
        public HttpResponseMessage Get(int id)
        {
            // using (var connection =
            //new SqlConnection(ConfigurationManager.ConnectionStrings["Chinook"].ConnectionString))
            // {
            try
            {
                // connection.Open()
                // var result =
                // connection.Query<EmployeeListResult>("Select * From Employee where EmployeeId = @id",
                //new {id = id}).FirstOrDefault();
                var repo = new EmployeeDataAccess();
                //repo is an instance of the EmployeeDataAccess class so it can use it's functions
                var result = repo.Get(id);

                if (result == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Employee with the Id {id} was not found"));
                }
                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
            // }
        }
        public HttpResponseMessage Get(int id)
        {
            try
            {
                var repo   = new EmployeeDataAccess();
                var result = repo.Get(id);

                if (result == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Couldn't find that employee"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        //api/employees/id
        public HttpResponseMessage Get(int id)
        {
            try
            {
                var employeeData = new EmployeeDataAccess();
                var employee     = employeeData.Get(id);

                if (employee == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Employee with the Id {id} was not found"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, employee));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }