示例#1
0
        public ActionResult Create(TaskViewModel task)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var taskDTO = _mapper.Map <TaskViewModel, TaskDTO>(task);
                    _taskService.SaveTask(taskDTO);
                    task.TaskId = _taskService.GetLastTaskId();

                    foreach (var item in task.SelectedUsers)
                    {
                        var userTask = new UserTaskViewModel
                        {
                            UserId       = Convert.ToInt32(item),
                            TaskId       = task.TaskId,
                            TaskName     = task.Name,
                            StartDate    = task.StartDate,
                            DeadlineDate = task.DeadLineDate,
                            State        = TaskStates.Active.ToString()
                        };
                        var userTaskDTO = _mapper.Map <UserTaskViewModel, UserTaskDTO>(userTask);
                        _vUserTaskService.SaveTaskForUser(userTaskDTO);
                    }
                    return(RedirectToAction("Index"));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return(View(task));
        }
示例#2
0
        public IHttpActionResult AddTask([FromBody] TaskViewModel task)
        {
            if (ModelState.IsValid)
            {
                var taskDTO = Mapper.Map <TaskViewModel, TaskDTO>(task);

                _taskService.SaveTask(taskDTO);

                task.TaskId = _taskService.GetLastTaskId();

                //why do we need to do it?
                foreach (var item in task.SelectedUsers)
                {
                    var userTask = new UserTaskViewModel
                    {
                        UserId       = Convert.ToInt32(item),
                        TaskId       = task.TaskId,
                        TaskName     = task.Name,
                        StartDate    = task.StartDate,
                        DeadlineDate = task.DeadLineDate,
                        State        = "Active"
                    };

                    var userTaskDTO = Mapper.Map <UserTaskViewModel, UserTaskDTO>(userTask);
                    _vUserTaskService.SaveTaskForUser(userTaskDTO);
                }

                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, "Task has been successfully created!")));
            }

            return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong! Please try again.")));
        }