Exemplo n.º 1
0
        public IHttpActionResult Post([FromBody] Employee employee)
        {
            try
            {
                List <string> errors = ValidateExtensions.ValidateEmployee(employee);

                if (errors.Count > 0)
                {
                    return(Content(HttpStatusCode.BadRequest, errors));
                }


                employee.Roles = TimeUnit.Roles.Get(employee.Roles.Id);
                employee.Image = employee.ConvertAndSave();
                TimeUnit.Employees.Insert(employee);
                TimeUnit.Save();
                Utility.Log($"EMPOLOYEE CONTROLLER: Post Called on Employee, Successfully added: {employee.FirstName + " " + employee.LastName}.", "INFO");
                return(Ok(TimeFactory.Create(employee)));
            }
            catch (Exception ex)
            {
                Utility.Log($"EMPOLOYEE CONTROLLER: Post Cannot be called on Employee.", "ERROR", ex);
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update Employee by Id.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="employee"></param>
        /// <returns></returns>
        /// <response code="200">Returns the newly-created item</response>
        /// <response code="400">If the item is null</response>

        public IHttpActionResult Put([FromBody] Employee employee, int id)
        {
            try
            {
                Employee oldEmployee = TimeUnit.Employees.Get(id);
                if (oldEmployee == null)
                {
                    return(NotFound());
                }
                employee = FillEmployeeWithOldData(employee, oldEmployee);

                employee.Roles = TimeUnit.Roles.Get(employee.Roles.Id);
                employee.Image = employee.ConvertAndSave();

                List <string> errors = ValidateExtensions.ValidateEmployee(employee);

                if (errors.Count > 0)
                {
                    return(Content(HttpStatusCode.BadRequest, errors));
                }

                if (TimeUnit.Employees.Get(id) == null)
                {
                    return(NotFound());
                }
                TimeUnit.Employees.Update(employee, id);
                TimeUnit.Save();
                Utility.Log($"EMPOLOYEE CONTROLLER: Put Called on Employee, Successfully updated id: {id}.", "INFO");
                return(Ok(TimeFactory.Create(employee)));
            }
            catch (Exception ex)
            {
                Utility.Log($"EMPOLOYEE CONTROLLER: Put Cannot be called on Employee.", "ERROR", ex);
                return(BadRequest(ex.Message));
            }
        }