-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
357 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package com.sap.todo.api; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.NotFoundException; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import com.sap.todo.dao.TaskDAO; | ||
import com.sap.todo.dao.TaskListDAO; | ||
import com.sap.todo.entity.Task; | ||
import com.sap.todo.entity.Tasklist; | ||
|
||
@Path("/tasklist") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class TasklistAPI { | ||
@Inject | ||
TaskListDAO taskListDAO; | ||
|
||
@Inject | ||
TaskDAO taskDAO; | ||
|
||
@GET | ||
@Path("/") | ||
public List<Tasklist> readAllTasklists() { | ||
return taskListDAO.findAll(); | ||
} | ||
|
||
|
||
@POST | ||
@Path("/") | ||
public Tasklist createTaskList(Tasklist tasklist) { | ||
return taskListDAO.persist(tasklist); | ||
} | ||
|
||
@POST | ||
@Path("/{id}/task/") | ||
public Task createTask(@PathParam("id") long listId, Task task) { | ||
Tasklist taskList = taskListDAO.find(listId); | ||
|
||
if (taskList == null) | ||
throw new NotFoundException(); | ||
|
||
taskList.addTask(task); | ||
return taskDAO.persist(task); | ||
} | ||
|
||
@PUT | ||
@Path("/{id}/task/{task_id}") | ||
public Task updateTaskOfTasklist(@PathParam("id") long listId, @PathParam("task_id") long taskId, Task task) { | ||
Task existingTask = taskDAO.findTaskByListIdAndTaskId(listId, taskId); | ||
if (existingTask == null) | ||
throw new NotFoundException(); | ||
|
||
task.setTaskList(existingTask.getTaskList()); | ||
task.setId(taskId); | ||
|
||
return taskDAO.merge(task); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
public Tasklist deleteTasklist(@PathParam("id") long listId) { | ||
Tasklist tasklist = taskListDAO.find(listId); | ||
if (tasklist == null) | ||
throw new NotFoundException(); | ||
|
||
return taskListDAO.remove(tasklist); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}/done_tasks") | ||
public List<Task> deleteTasklistDoneTasks(@PathParam("id") long listId) { | ||
Tasklist tasklist = taskListDAO.find(listId); | ||
if (tasklist == null) | ||
throw new NotFoundException(); | ||
|
||
ArrayList<Task> tasksToRemove = new ArrayList<>(); | ||
for (Task task : tasklist.getTasks()) { | ||
if (task.isDone()) | ||
tasksToRemove.add(task); | ||
} | ||
|
||
tasklist.getTasks().removeAll(tasksToRemove); | ||
taskDAO.remove(tasksToRemove); | ||
|
||
return tasksToRemove; | ||
} | ||
|
||
// | ||
// @GET | ||
// @Path("/{id}/task/{task_id}") | ||
// public Task readTask(@PathParam("id") long listId, @PathParam("task_id") long taskId) { | ||
// Task task = taskDAO.findTaskByListIdAndTaskId(listId, taskId); | ||
// | ||
// if (task == null) | ||
// throw new NotFoundException(); | ||
// | ||
// return task; | ||
// } | ||
|
||
|
||
// @GET | ||
// @Path("/{id}") | ||
// public Tasklist readTasklist(@PathParam("id") long id) { | ||
// Tasklist tasklist = taskListDAO.find(id); | ||
// if (tasklist == null) | ||
// throw new NotFoundException(); | ||
// | ||
// return tasklist; | ||
// } | ||
|
||
|
||
// @GET | ||
// @Path("/{id}/task/") | ||
// public List<Task> readAllTasks(@PathParam("id") long id) { | ||
// List<Task> tasklist = taskListDAO.findAllTasksByListId(id); | ||
// | ||
// if (tasklist == null) | ||
// throw new NotFoundException(); | ||
// | ||
// return tasklist; | ||
// } | ||
|
||
|
||
// @PUT | ||
// @Path("/{id}") | ||
// public Tasklist updateTasklist(@PathParam("id") long id, Tasklist tasklist) { | ||
// if (!taskListDAO.exists(id)) | ||
// throw new NotFoundException(); | ||
// | ||
// tasklist.setId(id); | ||
// return taskListDAO.merge(tasklist); | ||
// } | ||
|
||
|
||
// @DELETE | ||
// @Path("/{id}/task/{task_id}") | ||
// public Task deleteTask(@PathParam("id") long listId, @PathParam("task_id") long taskId) { | ||
// Task task = taskDAO.findTaskByListIdAndTaskId(listId, taskId); | ||
// if (task == null) | ||
// throw new NotFoundException(); | ||
// | ||
// Tasklist taskList = task.getTaskList(); | ||
// taskList.getTasks().remove(task); | ||
// | ||
// return taskDAO.remove(task); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.sap.todo.dao; | ||
|
||
import javax.persistence.NoResultException; | ||
|
||
import com.sap.todo.entity.Task; | ||
|
||
public class TaskDAO extends BaseDAO<Task, Long> { | ||
public TaskDAO() { | ||
super(Task.class); | ||
} | ||
|
||
public Task findTaskByListIdAndTaskId(long tasklistId, long taskId) { | ||
try { | ||
Task task = (Task) em.createQuery("select t from Task t where t.tasklist.id = :tasklistId and t.id = :taskId") | ||
.setParameter("tasklistId", tasklistId) | ||
.setParameter("taskId", taskId) | ||
.getSingleResult(); | ||
return task; | ||
} | ||
catch (NoResultException e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.sap.todo.dao; | ||
|
||
import com.sap.todo.entity.Tasklist; | ||
|
||
public class TaskListDAO extends BaseDAO<Tasklist, Long> { | ||
public TaskListDAO() { | ||
super(Tasklist.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.sap.todo.entity; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import javax.xml.bind.annotation.XmlTransient; | ||
|
||
@Entity | ||
@XmlRootElement | ||
public class Task { | ||
@Id | ||
@GeneratedValue | ||
private long id; | ||
|
||
private String description; | ||
private boolean done; | ||
|
||
@ManyToOne | ||
private Tasklist tasklist; | ||
|
||
public Task() { | ||
} | ||
|
||
public Task(Tasklist tasklist, String description) { | ||
this.tasklist = tasklist; | ||
this.description = description; | ||
this.done = true; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public void setDone(boolean done) { | ||
this.done = done; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public boolean isDone() { | ||
return done; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
@XmlTransient | ||
public Tasklist getTaskList() { | ||
return tasklist; | ||
} | ||
|
||
public void setTaskList(Tasklist taskList) { | ||
this.tasklist = taskList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.sap.todo.entity; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToMany; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
@Entity | ||
@XmlRootElement | ||
public class Tasklist { | ||
@Id | ||
@GeneratedValue | ||
private long id; | ||
|
||
private String name; | ||
private String icon; | ||
|
||
@OneToMany(cascade = CascadeType.REMOVE) | ||
private List<Task> tasks; | ||
|
||
public Tasklist() { | ||
} | ||
|
||
public Tasklist(String name, String icon) { | ||
tasks = new ArrayList<>(); | ||
this.name = name; | ||
this.icon = icon; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getIcon() { | ||
return icon; | ||
} | ||
|
||
public void setIcon(String icon) { | ||
this.icon = icon; | ||
} | ||
|
||
public List<Task> getTasks() { | ||
return tasks; | ||
} | ||
|
||
public void setTasks(List<Task> tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
public void addTask(Task task) { | ||
if (task.getTaskList() != this) | ||
task.setTaskList(this); | ||
tasks.add(task); | ||
} | ||
} |
Oops, something went wrong.