Beispiel #1
0
        public async Task <ActionResult <EmployeeTaskModel> > Put(int employee_id, CreateOrEditEmployeeTaskModel model)
        {
            if (model.StartDate.CompareTo(model.DeadLine) >= 0)
            {
                return(BadRequest("DeadLine should be larger than StartDate"));
            }
            if (employee_id != model.EmployeeId)
            {
                return(BadRequest("EmployeeId not match"));
            }
            try
            {
                var oldtask = await _repository.GetEmployeeTaskByIdAsync(employee_id, model.EmployeeTaskId);

                if (oldtask == null)
                {
                    return(NotFound($"Could not find task wiht id of {model.EmployeeTaskId}"));
                }

                var existTast = await _repository.GetEmployeeTaskByEmployeeAsync(employee_id, model.TaskName);

                if (existTast != null && existTast.EmployeeTaskId != model.EmployeeTaskId)
                {
                    return(BadRequest("Task have been existing"));
                }

                _mapper.Map(model, oldtask);

                if (await _repository.SaveChangesAsync())
                {
                    return(_mapper.Map <EmployeeTaskModel>(oldtask));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }
Beispiel #2
0
        public async Task <ActionResult <EmployeeTaskModel> > Post(int employee_id, CreateOrEditEmployeeTaskModel model)
        {
            try
            {
                var employee = await _repository.GetEmployeeByIdAsync(employee_id);

                if (employee == null)
                {
                    return(BadRequest("Employee does not exist"));
                }

                var existEmployeeTask = await _repository.GetEmployeeTaskByEmployeeAsync(employee_id, model.TaskName);

                if (existEmployeeTask != null)
                {
                    return(BadRequest("Task have been existing"));
                }

                var employeeTask = _mapper.Map <EmployeeTask>(model);

                employeeTask.EmployeeId = employee_id;

                _repository.Add(employeeTask);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"/api/Employees/{employee_id}/EmployeeTasks/{model.TaskName}", _mapper.Map <EmployeeTaskModel>(employeeTask)));
                }
                else
                {
                    return(BadRequest("Failed to save new EmployeeTask"));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }