Exemplo n.º 1
0
        // PUT api/employee/5
        public IHttpActionResult Put(string id, [FromBody] Employee employee)
        {
            try
            {
                MedicoFinalTestEntities dbcontext = new MedicoFinalTestEntities();

                var Employee = dbcontext.Employees.Where(x => x.Code == id).FirstOrDefault();
                if (Employee == null)
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content      = new StringContent("Employee not fount"),
                        ReasonPhrase = "Critical Exception"
                    });
                }

                Employee.Name    = string.IsNullOrEmpty(employee.Name) ? "" : employee.Name;
                Employee.Phone   = string.IsNullOrEmpty(employee.Phone) ? "" : employee.Phone;
                Employee.Address = string.IsNullOrEmpty(employee.Address) ? "" : employee.Address;
                dbcontext.SaveChanges();

                return(Ok(Employee));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(ex.Message.ToString()),
                    ReasonPhrase = "Critical Exception"
                });
            }
        }
Exemplo n.º 2
0
        // DELETE api/employee/5
        public IHttpActionResult Delete(string id)
        {
            try
            {
                MedicoFinalTestEntities dbcontext = new MedicoFinalTestEntities();

                var Employee = dbcontext.Employees.Where(x => x.Code == id).FirstOrDefault();
                if (Employee == null)
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content      = new StringContent("Employee not fount"),
                        ReasonPhrase = "Critical Exception"
                    });
                }

                dbcontext.Employees.Remove(Employee);
                dbcontext.SaveChanges();

                return(Ok(Employee));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(ex.Message.ToString()),
                    ReasonPhrase = "Critical Exception"
                });
            }
        }
Exemplo n.º 3
0
 // GET api/employee
 public IHttpActionResult Get()
 {
     try
     {
         MedicoFinalTestEntities dbcontext = new MedicoFinalTestEntities();
         var Employees = dbcontext.Employees.ToList();
         return(Ok(Employees));
     }
     catch (Exception ex)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content      = new StringContent(ex.Message.ToString()),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
Exemplo n.º 4
0
        // POST api/employee
        public IHttpActionResult Post([FromBody] Employee employee)
        {
            try
            {
                MedicoFinalTestEntities dbcontext = new MedicoFinalTestEntities();

                employee.Code    = Guid.NewGuid().ToString();
                employee.Name    = string.IsNullOrEmpty(employee.Name) ? "" : employee.Name;
                employee.Phone   = string.IsNullOrEmpty(employee.Phone) ? "" : employee.Phone;
                employee.Address = string.IsNullOrEmpty(employee.Address) ? "" : employee.Address;
                dbcontext.Employees.Add(employee);
                dbcontext.SaveChanges();

                return(Ok(employee));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(ex.Message.ToString()),
                    ReasonPhrase = "Critical Exception"
                });
            }
        }