aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java7
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java1
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java10
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java14
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java13
5 files changed, 35 insertions, 10 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
index 9592490..36ae341 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
@@ -17,10 +17,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
+import com.ufund.api.ufundapi.DuplicateKeyException;
import com.ufund.api.ufundapi.model.Need;
import com.ufund.api.ufundapi.model.Need.GoalType;
import com.ufund.api.ufundapi.service.CupboardService;
-import com.ufund.api.ufundapi.DuplicateKeyException;
@RestController
@RequestMapping("cupboard")
@@ -50,7 +50,7 @@ public class CupboardController {
public ResponseEntity<Need> createNeed(@RequestBody Map<String, Object> params) {
System.out.println(params);
String name = (String) params.get("name");
- int maxGoal = (int) params.get("maxGoal");
+ double maxGoal = (double) params.get("maxGoal");
Need.GoalType goalType = GoalType.valueOf((String) params.get("type"));
try {
@@ -59,7 +59,7 @@ public class CupboardController {
} catch (DuplicateKeyException ex) {
return new ResponseEntity<>(HttpStatus.CONFLICT);
} catch (IllegalArgumentException ex) {
- return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
+ return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (IOException ex) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@@ -144,6 +144,7 @@ public class CupboardController {
*/
@PutMapping("/{id}")
public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id) {
+ LOG.log(Level.INFO, "Updating need: " + need);
try {
Need updatedNeed = cupboardService.updateNeed(need, id);
if (updatedNeed != null) {
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
index adf17a1..fd6a960 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
@@ -102,6 +102,7 @@ public class UserController {
*/
@PutMapping("/{username}")
public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) {
+ LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key.toString());
try {
authService.authenticate(username, key);
user = userService.updateUser(user, username);
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
index 1c1d474..6de1a8a 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
@@ -51,12 +51,12 @@ public class User {
basket.add(need.getId());
}
- public Integer[] getBasketNeeds() {
+ public Integer[] getNeeds() {
return basket.toArray(Integer[]::new);
}
- public void removeBasketNeed(Need need) {
- basket.remove(need.getId());
+ public boolean removeBasketNeed(Integer needID) {
+ return basket.remove(needID);
}
public User withoutPasswordHash() {
@@ -71,4 +71,8 @@ public class User {
this.passwordHash = other.passwordHash;
}
+ public String toString() {
+ return this.username + "; basket: " + this.basket + "; type:" + this.type + "; hash: " + this.passwordHash;
+ }
+
}
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
index f809aac..6e900aa 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
@@ -2,6 +2,7 @@ package com.ufund.api.ufundapi.persistence;
import java.io.File;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -81,8 +82,17 @@ public class UserFileDAO implements UserDAO {
public User updateUser(User user) throws IOException {
synchronized (users) {
if (users.containsKey(user.getUsername())) {
- var old = users.put(user.getUsername(), user);
- user.copyPassword(old);
+ // var old = users.put(user.getUsername(), user);
+ // user.copyPassword(old);
+ if (user.getNeeds() == null || user.getType() == null) {
+ User oldData = users.get(user.getUsername());
+ User crutch = new User(oldData.getUsername(), 0, new ArrayList<Integer>(), oldData.getType());
+ crutch.copyPassword(oldData);
+ users.put(user.getUsername(), crutch);
+ } else {
+ var old = users.put(user.getUsername(), user);
+ user.copyPassword(old);
+ }
save();
return user;
} else {
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
index 935ee72..caf9f4c 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
@@ -2,18 +2,21 @@ package com.ufund.api.ufundapi.service;
import java.io.IOException;
+import org.springframework.stereotype.Component;
+
import com.ufund.api.ufundapi.DuplicateKeyException;
import com.ufund.api.ufundapi.model.User;
import com.ufund.api.ufundapi.persistence.UserDAO;
-import org.springframework.stereotype.Component;
@Component
public class UserService {
private final UserDAO userDAO;
+ private final CupboardService cupboardService;
- public UserService(UserDAO userDao) {
+ public UserService(UserDAO userDao, CupboardService cupboardService) {
this.userDAO = userDao;
+ this.cupboardService = cupboardService;
}
/**
@@ -40,6 +43,12 @@ public class UserService {
* @throws IOException If there was any problem saving the file
*/
public User getUser(String username) throws IOException {
+ User user = userDAO.getUser(username);
+ for (int needId : user.getNeeds()) {
+ if (cupboardService.getNeed(needId) == null) {
+ user.removeBasketNeed(needId);
+ }
+ }
return userDAO.getUser(username);
}