Skip to content

Commit a8b03bb

Browse files
author
BuildTools
committed
Removed unnecessary login page and references, simplified habit controller, added better interface for user habit handling
1 parent 5ec2caf commit a8b03bb

File tree

8 files changed

+38
-226
lines changed

8 files changed

+38
-226
lines changed

src/main/java/com/justin/healthyhabits/controllers/HabitsController.java

+11-39
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.springframework.web.bind.annotation.ModelAttribute;
1212
import org.springframework.web.bind.annotation.PostMapping;
1313

14-
import java.util.ArrayList;
15-
import java.util.Map;
1614
import java.util.NoSuchElementException;
1715

1816
@Controller
@@ -35,51 +33,25 @@ public String habitForm(Model model) {
3533
@PostMapping("/habits")
3634
public String habitSubmit(@ModelAttribute Habit habit, Model model) {
3735
habit.setName(habit.getName().trim());
36+
3837
model.addAttribute("habit", habit);
3938
model.addAttribute("user", userd.getUser());
39+
40+
String habitSubmissionType = habit.getHabitType();
41+
boolean habitExists = userd.getUser().hasHabit(habit.getName());
42+
4043
try {
41-
if (habit.getHabitType().equals("add")) {
42-
if (!DataValidation.isHabitValid((habit))) return "errorpage";
43-
else {
44-
if (habitExists(habit)) updateHabit(habit);
45-
else addNewHabit(habit);
46-
}
47-
} else if (habit.getHabitType().equals("remove")) {
48-
userd.getUser().getHabits().remove(habit.getName());
44+
if (!DataValidation.isHabitValid((habit))) return "errorpage";
45+
if (habitSubmissionType.equals("add")) {
46+
if (habitExists) userd.getUser().saveHabit(habit);
47+
else userd.getUser().addHabit(habit);
48+
} else if (habitSubmissionType.equals("remove")) {
49+
userd.getUser().removeHabit(habit.getName());
4950
}
5051
userService.saveUser(userd.getUser());
51-
5252
} catch (NullPointerException | NoSuchElementException e) {
5353
return "errorpage";
5454
}
5555
return "habitspage";
5656
}
57-
58-
private Map<String, Habit> getUserHabits() {
59-
return userd.getUser().getHabits();
60-
}
61-
62-
@SuppressWarnings("unchecked")
63-
private void addNewHabit(Habit habit) {
64-
ArrayList<Long> dates = new ArrayList<>();
65-
dates.add(getDate());
66-
habit.setDates(dates);
67-
userd.getUser().getHabits().put(habit.getName(), habit);
68-
}
69-
70-
private boolean habitExists(Habit habit) {
71-
return getUserHabits().containsKey(habit.getName());
72-
}
73-
74-
private void updateHabit(Habit habit) {
75-
if (getUserHabits().get(habit.getName()) != null) {
76-
userd.getUser().getHabits().get(habit.getName()).addDate(getDate());
77-
userd.getUser().getHabits().get(habit.getName()).addRating(habit.getRatings().get(0));
78-
userService.saveUser(userd.getUser());
79-
}
80-
}
81-
82-
private long getDate() {
83-
return System.currentTimeMillis();
84-
}
8557
}

src/main/java/com/justin/healthyhabits/controllers/LoginController.java

-37
This file was deleted.

src/main/java/com/justin/healthyhabits/user/User.java

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.justin.healthyhabits.user;
22

33
import javax.persistence.*;
4+
import java.util.ArrayList;
45
import java.util.Map;
56

67
@Entity
@@ -61,4 +62,30 @@ public void setHabits(Map<String, Habit> habits) {
6162
public void setPassword(String password) {
6263
this.password = password;
6364
}
65+
66+
public void addHabit(Habit habit) {
67+
ArrayList<Long> dates = new ArrayList<>();
68+
dates.add(System.currentTimeMillis());
69+
habit.setDates(dates);
70+
getHabits().put(habit.getName(), habit);
71+
}
72+
73+
public Habit removeHabit(String habitId) {
74+
return getHabits().remove(habitId);
75+
}
76+
77+
public boolean saveHabit(Habit habit) {
78+
boolean habitExists = getHabits().get(habit.getName()) != null;
79+
if (habitExists) {
80+
getHabits().get(habit.getName()).addDate(System.currentTimeMillis());
81+
getHabits().get(habit.getName()).addRating(habit.getRatings().get(0));
82+
return true;
83+
}
84+
return false;
85+
}
86+
87+
public boolean hasHabit(String habitId) {
88+
return getHabits().containsKey(habitId);
89+
}
90+
6491
}

src/main/resources/templates/errorpage.html

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
<a class="navbar-link" href="">Account</a>
3232
</div>
3333
<div class="dropdown-content">
34-
<a href="http://justinm.tech/login">Login</a>
3534
<a href="http://justinm.tech/logout">Logout</a>
3635
<a href="http://justinm.tech/register">Register</a>
3736
</div>

src/main/resources/templates/home.html

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<a class="navbar-link" href="">Account</a>
3030
</div>
3131
<div class="dropdown-content">
32-
<a href="http://justinm.tech/login">Login</a>
3332
<a href="http://justinm.tech/logout">Logout</a>
3433
<a href="http://justinm.tech/register">Register</a>
3534
</div>

src/main/resources/templates/login.html

-78
This file was deleted.

src/main/resources/templates/loginsuccessful.html

-69
This file was deleted.

src/main/resources/templates/register.html

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
<a class="navbar-link" href="">Account</a>
3232
</div>
3333
<div class="dropdown-content">
34-
<a href="http://justinm.tech/login">Login</a>
3534
<a href="http://justinm.tech/register">Register</a>
3635
</div>
3736
</div>

0 commit comments

Comments
 (0)