Skip to content
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

Merged
merged 7 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/main/java/lv/ctco/javaschool/goal/boundary/GoalApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -152,7 +154,6 @@ public List<TagDto> returnAllTags() {
}

@POST
@RolesAllowed({"ADMIN", "USER"})
Copy link
Contributor

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?

Copy link
Contributor Author

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 .

Copy link
Contributor

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.

@Path("/search-goals")
public List<GoalDto> getGoalsByTag(JsonObject searchDto) {
List<GoalDto> goalDtoList = new ArrayList<>();
Expand All @@ -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<>();
Expand All @@ -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);
Expand All @@ -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().equals(user)) {
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
Expand Up @@ -25,6 +25,7 @@ public static GoalDto convertGoalToGoalDto(Goal goal) {
dto.setDaysLeft(DateTimeConverter.countDaysLeft(goal.getDeadlineDate()));
dto.setId(goal.getId());
dto.setTags(goal.getTags());
dto.setGoalStatus(goal.getStatus());
return dto;
}

Expand All @@ -38,6 +39,7 @@ public static UserLoginDto convertUserToUserLoginDto(User user) {

public static CommentDto convertCommentToCommentDto(Comment comment) {
CommentDto dto = new CommentDto();
dto.setUserId(comment.getUser().getId());
dto.setUsername(comment.getUser().getUsername());
dto.setRegisteredDate(comment.getRegisteredDate().format(DateTimeConverter.FORMATTER_DATE_TIME));
dto.setCommentMessage(comment.getCommentMessage());
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/lv/ctco/javaschool/goal/control/GoalStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,12 +22,27 @@ 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<>();
LocalDate localDateNow = LocalDate.now();
for (Goal goal : goals) {
if ((goal.getDeadlineDate().isBefore(localDateNow)
|| goal.getDeadlineDate().isEqual(localDateNow))
&& goal.getStatus() != GoalStatus.ACHIEVED) {
goal.setStatus(GoalStatus.OVERDUE);
}
goalsToReturn.add(goal);
}
return goalsToReturn;
}

public void addGoal(Goal goal) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/lv/ctco/javaschool/goal/entity/domain/Goal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lv.ctco.javaschool.auth.entity.domain.User;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
Expand Down Expand Up @@ -31,6 +33,8 @@ public class Goal {
private String goalMessage;
private LocalDate deadlineDate;
private LocalDateTime registeredDate;
@Enumerated(EnumType.STRING)
private GoalStatus status;

public Goal() {
}
Expand All @@ -44,6 +48,14 @@ public Goal(Long id, User user, Set<Tag> tags, String goalMessage, LocalDate dea
this.registeredDate = registeredDate;
}

public GoalStatus getStatus() {
return status;
}

public void setStatus(GoalStatus status) {
this.status = status;
}

public Set<Tag> getTags() {
return tags;
}
Expand Down Expand Up @@ -91,4 +103,33 @@ public LocalDateTime getRegisteredDate() {
public void setRegisteredDate(LocalDateTime registeredDate) {
this.registeredDate = registeredDate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Goal goal = (Goal) o;

if (id != null ? !id.equals(goal.id) : goal.id != null) return false;
if (user != null ? !user.equals(goal.user) : goal.user != null) return false;
if (tags != null ? !tags.equals(goal.tags) : goal.tags != null) return false;
if (goalMessage != null ? !goalMessage.equals(goal.goalMessage) : goal.goalMessage != null) return false;
if (deadlineDate != null ? !deadlineDate.equals(goal.deadlineDate) : goal.deadlineDate != null) return false;
if (registeredDate != null ? !registeredDate.equals(goal.registeredDate) : goal.registeredDate != null)
return false;
return status == goal.status;
}

@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (user != null ? user.hashCode() : 0);
result = 31 * result + (tags != null ? tags.hashCode() : 0);
result = 31 * result + (goalMessage != null ? goalMessage.hashCode() : 0);
result = 31 * result + (deadlineDate != null ? deadlineDate.hashCode() : 0);
result = 31 * result + (registeredDate != null ? registeredDate.hashCode() : 0);
result = 31 * result + (status != null ? status.hashCode() : 0);
return result;
}
}
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
}
13 changes: 11 additions & 2 deletions src/main/java/lv/ctco/javaschool/goal/entity/dto/CommentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

public class CommentDto {
private String username;

private Long userId;
private String registeredDate;
private String commentMessage;

public CommentDto() {
}

public CommentDto(String username, String localDateTime, String msg) {
public CommentDto(String username, String localDateTime, String msg, Long id) {
this.username = username;
this.registeredDate = localDateTime;
this.commentMessage = msg;
this.userId = id;
}

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public String getUsername() {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/lv/ctco/javaschool/goal/entity/dto/GoalDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lv.ctco.javaschool.goal.entity.dto;

import lv.ctco.javaschool.goal.entity.domain.GoalStatus;
import lv.ctco.javaschool.goal.entity.domain.Tag;

import java.time.LocalDate;
Expand All @@ -15,6 +16,15 @@ public class GoalDto {
private LocalDateTime registeredDate;
private Set<Tag> tags;
private int daysLeft;
private GoalStatus goalStatus;

public GoalStatus getGoalStatus() {
return goalStatus;
}

public void setGoalStatus(GoalStatus goalStatus) {
this.goalStatus = goalStatus;
}

public Long getUserId() {
return userId;
Expand Down
14 changes: 13 additions & 1 deletion src/main/webapp/app/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,21 @@ ul {
margin-bottom: 5%;
}

.ACHIEVED {
background-color: #27ff5a !important;
}

.OVERDUE {
background-color: #ff5953 !important;
}

input[type=date]::-webkit-clear-button,
input[type=date]::-webkit-inner-spin-button,
input[type=date]::-webkit-outer-spin-button {
display: none;
-webkit-appearance: none;
}
}

span.ACHIEVED {
visibility: hidden !important;
}
2 changes: 1 addition & 1 deletion src/main/webapp/app/findgoals.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<th>Deadline</th>
<th>Days left</th>
</tr>
<tr w3-repeat="goals" id="{{id}}" onclick="redirectToGoalsAndComments(id)">
<tr w3-repeat="goals" id="{{id}}" class="{{goalStatus}}" onclick="redirectToGoalsAndComments(id)">
<td>{{goalMessage}}</td>
<td>{{deadlineDate}}</td>
<td>{{daysLeft}}</td>
Expand Down
12 changes: 8 additions & 4 deletions src/main/webapp/app/goal.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
<div class="button-div">
<button id="edit-button" class="menu-button" onclick="editGoal()" type="button">Edit Goal</button>
</div>
<div class="button-div" id="status-achieved">
<button class="menu-button" onclick="setStatusAchieved('{{id}}')" type="button">Achieved</button>
</div>
</div>
<div id="goal-fields">
<div id="show-goal">
<h3>Goal: {{goalMessage}}</h3>
<h5>Author: <a onclick="redirectToUserById('{{userId}}')">{{username}}</a></h5>
<h5>Deadline: {{deadlineDate}} (days left: {{daysLeft}})</h5>
<h5>Deadline: {{deadlineDate}}</h5>
<h5 class="{{goalStatus}}">Goal status: {{goalStatus}}</h5>
<div id="tags-list" w3-repeat="tags">
<span id="{{id}}">{{tagMessage}}</span>
</div>
Expand Down Expand Up @@ -61,12 +65,12 @@
<hr>
<ul id="sortable" class="w3-hide">
<li w3-repeat="comments" class="repeatable">
<strong>{{username}}</strong>
<strong> <a onclick="redirectToUserById('{{userId}}')">{{username}}</a></strong>
<small>
<span class="glyphicon glyphicon-time"></span>{{registeredDate}}
</small>
<br/>
<br/>
</br>
</br>
<p>{{commentMessage}}</p>
</li>
</ul>
Expand Down
4 changes: 0 additions & 4 deletions src/main/webapp/app/js/findgoals-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function findUserByName() {
var dto = {
"usersearch": username.value
};
console.log(JSON.stringify(dto));
fetch("/api/goal/search-user", {
"method": "POST",
headers: {
Expand All @@ -34,7 +33,6 @@ function findUserByName() {
}).then(function (response) {
return response.json();
}).then(function (users) {
console.log(users);
if (users.length > 0) {
var tabledata = {'users': users};
document.getElementById("Users-List").classList.remove("w3-hide");
Expand All @@ -49,7 +47,6 @@ function findGoalsByTag() {
var dto = {
"goalsearch": tag.value
};
console.log(JSON.stringify(dto));
fetch("/api/goal/search-goals", {
"method": "POST",
headers: {
Expand All @@ -60,7 +57,6 @@ function findGoalsByTag() {
}).then(function (response) {
return response.json();
}).then(function (goals) {
console.log(goals);
if (goals.length > 0) {
var tabledata = {'goals': goals};
document.getElementById("Goals-List").classList.remove("w3-hide");
Expand Down
Loading