Adding a new task

Only admin users can add a new task. This controller method will insert the task record in the database. It looks as follows:

@PostMapping("/addTask")
fun addTask(@RequestParam(name = "title",required = true) title:String,
@RequestParam(name = "detail",required = true) detail:String,
@RequestParam(name = "selectedUserId", required = true) selectedUserId:Int,
model:Model):String {
val task = Task()
task.setTitle(title)
task.setDetail(detail)
task.setAssignedTo(selectedUserId)
task.setStatus(TaskStatus.PENDING.getStatus())
taskRepository?.save(task)

logger.info("Goint to show Add task page")
setProcessingData(model, TaskMgmntConstant.TITLE_ADD_TASK_PAGE)
model.addAttribute("screenTitle","Add new Task")
return "redirect:allTaskList"
}

The title, detail, and userId parameters (to whom the task is assigned) are provided from the add task screen. This method simply creates an instance of the Task class, populates its value, and saves it in taskRepsitory. Unlike Java, the instance can be created without the new keyword in Kotlin. Also, Kotlin defers the type of variable wherever it is possible. For example, we have not defined the type of the task variable because it is assigned the object of the Task type class so Kotlin understands that it is the Task type only.

Instead of redirecting to a specific page, we are redirecting to another controller method with the /allTaskList URL pattern, which basically shows a task list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.53.209