-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Status goals #56
Status goals #56
Changes from 3 commits
888a3cd
9e4bab6
d248106
f6987ae
6c1e80c
969e4bf
e052693
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import lv.ctco.javaschool.goal.control.TagParser; | ||
import lv.ctco.javaschool.goal.entity.domain.Comment; | ||
import lv.ctco.javaschool.goal.entity.domain.Goal; | ||
import lv.ctco.javaschool.goal.entity.domain.GoalStatus; | ||
import lv.ctco.javaschool.goal.entity.domain.Tag; | ||
import lv.ctco.javaschool.goal.entity.dto.CommentDto; | ||
import lv.ctco.javaschool.goal.entity.dto.GoalDto; | ||
|
@@ -17,6 +18,7 @@ | |
import lv.ctco.javaschool.goal.entity.dto.UserDto; | ||
import lv.ctco.javaschool.goal.entity.exception.InvalidGoalException; | ||
import lv.ctco.javaschool.goal.entity.exception.InvalidUserException; | ||
import lv.ctco.javaschool.goal.entity.exception.ValidationException; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import javax.ejb.Stateless; | ||
|
@@ -81,7 +83,7 @@ public void createNewGoal(GoalFormDto goalDto) { | |
goal.setTags(tagSet); | ||
|
||
goal.setDeadlineDate(goalDto.getDeadline()); | ||
|
||
goal.setStatus(GoalStatus.OPEN); | ||
goal.setUser(user); | ||
goal.setRegisteredDate(LocalDateTime.now()); | ||
goalStore.addGoal(goal); | ||
|
@@ -152,7 +154,6 @@ public List<TagDto> returnAllTags() { | |
} | ||
|
||
@POST | ||
@RolesAllowed({"ADMIN", "USER"}) | ||
@Path("/search-goals") | ||
public List<GoalDto> getGoalsByTag(JsonObject searchDto) { | ||
List<GoalDto> goalDtoList = new ArrayList<>(); | ||
|
@@ -173,7 +174,6 @@ public List<GoalDto> getGoalsByTag(JsonObject searchDto) { | |
} | ||
|
||
@POST | ||
@RolesAllowed({"ADMIN", "USER"}) | ||
@Path("/search-user") | ||
public List<UserSearchDto> getUsersByUsername(JsonObject searchDto) { | ||
List<UserSearchDto> userDtoList = new ArrayList<>(); | ||
|
@@ -191,7 +191,6 @@ public List<UserSearchDto> getUsersByUsername(JsonObject searchDto) { | |
} | ||
|
||
@GET | ||
@RolesAllowed({"ADMIN", "USER"}) | ||
@Path("/user/{id}") | ||
public UserDto getUserById(@PathParam("id") Long id) { | ||
Optional<User> user = userStore.findUserById(id); | ||
|
@@ -204,5 +203,22 @@ public UserDto getUserById(@PathParam("id") Long id) { | |
throw new InvalidUserException(); | ||
} | ||
} | ||
|
||
@POST | ||
@Path("/mygoals/{id}") | ||
public void setStatusAchieved(@PathParam("id") Long goalId) { | ||
User user = userStore.getCurrentUser(); | ||
Optional<Goal> goal = goalStore.getGoalById(goalId); | ||
if (goal.isPresent()) { | ||
Goal g = goal.get(); | ||
if (g.getUser() == user) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only primitives, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be possible that they are the same object due to entity persistence framework (Hibernate), however we cannot rely on that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added equals() and hashCode() |
||
g.setStatus(GoalStatus.ACHIEVED); | ||
} else { | ||
throw new ValidationException("Current Goal does not belong to you so you can not Edit Status"); | ||
} | ||
} else { | ||
throw new ValidationException("The Goal does not exist"); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,14 @@ | |
import lv.ctco.javaschool.auth.entity.domain.User; | ||
import lv.ctco.javaschool.goal.entity.domain.Comment; | ||
import lv.ctco.javaschool.goal.entity.domain.Goal; | ||
import lv.ctco.javaschool.goal.entity.domain.GoalStatus; | ||
import lv.ctco.javaschool.goal.entity.domain.Tag; | ||
|
||
import javax.ejb.Stateless; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
@@ -19,12 +22,26 @@ public class GoalStore { | |
private EntityManager em; | ||
|
||
public List<Goal> getGoalsListFor(User user) { | ||
return em.createQuery( | ||
List<Goal> goals = em.createQuery( | ||
"select g " + | ||
"from Goal g " + | ||
"where g.user = :user ", Goal.class) | ||
.setParameter("user", user) | ||
.getResultList(); | ||
return CheckStatus(goals); | ||
} | ||
|
||
private List<Goal> CheckStatus(List<Goal> goals) { | ||
List<Goal> goalsToReturn = new ArrayList<>(); | ||
for (Goal goal : goals) { | ||
if ((goal.getDeadlineDate().isBefore(LocalDate.now()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please extract There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extracted thank you. |
||
|| goal.getDeadlineDate().isEqual(LocalDate.now())) | ||
&& goal.getStatus() != GoalStatus.ACHIEVED) { | ||
goal.setStatus(GoalStatus.OVERDUE); | ||
} | ||
goalsToReturn.add(goal); | ||
} | ||
return goalsToReturn; | ||
} | ||
|
||
public void addGoal(Goal goal) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lv.ctco.javaschool.goal.entity.domain; | ||
|
||
public enum GoalStatus { | ||
OPEN, | ||
ACHIEVED, | ||
OVERDUE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,8 +38,12 @@ function onLoad() { | |
}).then(function (goal) { | ||
w3.displayObject("title", goal); | ||
w3.displayObject("goal-fields", goal); | ||
w3.displayObject("status-achieved", goal); | ||
w3DisplayData("tags-list", goal); | ||
document.getElementById("goal-deadline").setAttribute("value", goal.deadlineDate); | ||
if (goal.goalStatus === "ACHIEVED") { | ||
document.getElementById("status-achieved").classList.add("w3-hide"); | ||
} | ||
getComments(); | ||
}); | ||
enableEditForGoalOwner(); | ||
|
@@ -94,8 +98,10 @@ function enableEditForGoalOwner() { | |
return false; | ||
} | ||
}).then(function (goalEdit) { | ||
if (!goalEdit) | ||
if (!goalEdit) { | ||
document.getElementById("edit-button").classList.add("w3-hide"); | ||
document.getElementById("status-achieved").classList.add("w3-hide"); | ||
} | ||
isMyGoal = goalEdit; | ||
}); | ||
} | ||
|
@@ -104,6 +110,8 @@ function editGoal() { | |
if (isMyGoal) { | ||
document.getElementById("show-goal").classList.add("w3-hide"); | ||
document.getElementById("edit-goal").classList.remove("w3-hide"); | ||
document.getElementById("edit-button").classList.add("w3-hide"); | ||
document.getElementById("status-achieved").classList.add("w3-hide"); | ||
} else { | ||
alert("You can edit only your own goals"); | ||
} | ||
|
@@ -135,4 +143,22 @@ function saveEditGoal() { | |
return false; | ||
} | ||
}); | ||
} | ||
|
||
function setStatusAchieved(id) { | ||
fetch(path + "/api/goal/mygoals/" + id, { | ||
"method": "POST", | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}).then(function (response) { | ||
if (response.status === 204) { | ||
onLoad(); | ||
} else { | ||
console.log(response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth it to extract There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alert() extracted to function displayErrors() and removed all console.log() |
||
alert("Something went wrong! error: " + response.status.toString()); | ||
return false; | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this removed? Do you want to allow users without these roles to use these methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they were moved to the top of the GoalApi because that all the methods are allowed to all users in GoalApi .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Did not see the class-level annotation at first, due to GitHub collapsing unchanged code.