aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ufund-api/data/userAuths.json2
-rw-r--r--ufund-api/data/users.json15
-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
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java8
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java2
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java18
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java4
-rw-r--r--ufund-ui/src/app/components/cupboard/cupboard.component.ts205
-rw-r--r--ufund-ui/src/app/components/dashboard/dashboard.component.html3
-rw-r--r--ufund-ui/src/app/components/dashboard/dashboard.component.ts14
-rw-r--r--ufund-ui/src/app/components/funding-basket/funding-basket.component.css7
-rw-r--r--ufund-ui/src/app/components/funding-basket/funding-basket.component.html28
-rw-r--r--ufund-ui/src/app/components/funding-basket/funding-basket.component.ts109
-rw-r--r--ufund-ui/src/app/components/need-list/need-list.component.html2
-rw-r--r--ufund-ui/src/app/components/need-list/need-list.component.ts25
-rw-r--r--ufund-ui/src/app/components/need-page/need-page.component.html3
-rw-r--r--ufund-ui/src/app/services/users.service.ts36
21 files changed, 291 insertions, 235 deletions
diff --git a/ufund-api/data/userAuths.json b/ufund-api/data/userAuths.json
index fea8a6f..b703ae2 100644
--- a/ufund-api/data/userAuths.json
+++ b/ufund-api/data/userAuths.json
@@ -1 +1 @@
-[{"key":"e48872fa-b89f-494a-b681-11a809d32ff4","username":"phil","expiration":"2025-04-14T17:20:23.265745224"},{"key":"31fcbc15-9902-41d2-8d6f-5b0e40ebddd2","username":"phil","expiration":"2025-04-14T16:45:41.082560826"},{"key":"3fdd4e7e-bc59-4e3a-ba5c-177d0833022a","username":"sowgro","expiration":"2025-04-14T18:35:26.42935739"},{"key":"13d12a6d-6825-4c1d-8b22-ba960de140b8","username":"phil","expiration":"2025-04-14T17:20:58.531711142"},{"key":"a07ae51f-f80b-4001-95f1-48c11d4917a4","username":"phil","expiration":"2025-04-05T15:04:30.900359001"},{"key":"cc49c007-fd36-4828-b8fa-f5b85ad0676d","username":"phil","expiration":"2025-04-14T16:46:12.80566798"},{"key":"da61796e-402a-4a80-88ae-7607a37989a4","username":"phil","expiration":"2025-04-14T17:07:10.618039573"},{"key":"e14f8ee5-5780-4b9b-bf34-7a41c2bbfcb4","username":"phil","expiration":"2025-04-05T13:46:10.90733016"},{"key":"d7cef571-0f76-49fe-941f-ecbeae69557a","username":"phil","expiration":"2025-04-05T15:14:00.363201102"},{"key":"125a4847-3a1c-4834-961f-7f96e997f92e","username":"sowgro","expiration":"2025-04-14T18:35:38.922687686"},{"key":"3fc557b6-0306-4779-9b74-b7292a5cf1cc","username":"phil","expiration":"2025-04-14T16:06:08.564069822"},{"key":"77392d17-6e0c-45ec-857d-6595a55ddd97","username":"phil","expiration":"2025-04-14T16:06:48.335542315"},{"key":"eeea7b02-7265-4a26-96de-a8ad1860c533","username":"phil","expiration":"2025-03-31T23:04:47.455490668"},{"key":"e121c7c6-e534-4fde-8a78-4f175e9db9c8","username":"phil","expiration":"2025-04-14T17:23:23.218442063"},{"key":"af38add5-b100-4b96-9ffb-5afaccd59979","username":"adf","expiration":"2025-04-14T18:18:47.670506361"},{"key":"2aeaab28-99c9-4b45-bdef-82096c70945e","username":"phil","expiration":"2025-04-14T17:19:48.552121268"},{"key":"58e4e2a2-3a36-4fd6-8bb1-ad0831664d01","username":"phil","expiration":"2025-04-12T23:17:42.638952959"},{"key":"4df8bb43-f597-49ca-863a-6e0da5280d79","username":"phil","expiration":"2025-04-14T01:13:53.799331844"},{"key":"ad6d92d4-c496-407c-823a-edaa386e67ed","username":"phil","expiration":"2025-04-14T17:07:36.032623002"},{"key":"718be1e2-cfc7-44a6-b3c6-965684d1d0a9","username":"adf","expiration":"2025-04-14T18:35:58.888847176"},{"key":"85319427-4603-4a16-af33-2e9525dda8c0","username":"phil","expiration":"2025-04-14T00:39:34.952183453"},{"key":"f5f53053-ef5e-4850-93a0-3dc20646f78b","username":"sowgro","expiration":"2025-04-14T18:11:29.438554549"},{"key":"efc531fb-ab24-4d5a-a2f5-7f4ede74819f","username":"phil","expiration":"2025-04-13T19:41:51.017327545"},{"key":"f1d6a110-4232-4ef3-b6ec-9a2962664158","username":"phil","expiration":"2025-04-14T17:23:40.834526839"}] \ No newline at end of file
+[{"key":"3fdd4e7e-bc59-4e3a-ba5c-177d0833022a","username":"sowgro","expiration":"2025-04-14T18:35:26.42935739"},{"key":"5d0182e2-247e-4b4e-b165-cd95710c2402","username":"phil","expiration":"2025-04-17T08:17:09.1886771"},{"key":"13d12a6d-6825-4c1d-8b22-ba960de140b8","username":"phil","expiration":"2025-04-14T17:20:58.531711142"},{"key":"960ef9a5-021d-49a1-a752-ad9dfd8a40f3","username":"phil","expiration":"2025-04-17T10:53:51.2661748"},{"key":"20529784-361d-4111-8b2e-13a23b24d6cc","username":"phil","expiration":"2025-04-17T09:07:11.4534752"},{"key":"342977bc-2095-4325-96d8-ea4cdaa241c6","username":"phil","expiration":"2025-04-17T08:47:07.5445119"},{"key":"e2c17a24-ad11-4e63-8b01-74baf3ae6b4a","username":"phil","expiration":"2025-04-17T10:14:03.2239052"},{"key":"f9f6bc24-7a69-4bfe-92c3-c1166d20f0db","username":"phil","expiration":"2025-04-17T10:50:51.9716943"},{"key":"1dc114a0-f5d7-410e-8664-2ca9af3393ac","username":"phil","expiration":"2025-04-17T09:06:49.5204299"},{"key":"ff1e80a8-344f-4578-9ead-a6d46c8ed1c2","username":"phil","expiration":"2025-04-17T10:51:00.0607854"},{"key":"eeea7b02-7265-4a26-96de-a8ad1860c533","username":"phil","expiration":"2025-03-31T23:04:47.455490668"},{"key":"e121c7c6-e534-4fde-8a78-4f175e9db9c8","username":"phil","expiration":"2025-04-14T17:23:23.218442063"},{"key":"4df8bb43-f597-49ca-863a-6e0da5280d79","username":"phil","expiration":"2025-04-14T01:13:53.799331844"},{"key":"05e8790e-67fa-45de-adfe-82c6f5fdd15b","username":"phil","expiration":"2025-04-17T08:18:05.2696558"},{"key":"4eafe9d9-1b05-4fbd-90f1-c7f856d338dd","username":"phil","expiration":"2025-04-17T09:46:48.0123639"},{"key":"1a9b7e5a-d19c-43ef-bb02-838b6fc695e0","username":"phil","expiration":"2025-04-17T09:37:48.4955941"},{"key":"718be1e2-cfc7-44a6-b3c6-965684d1d0a9","username":"adf","expiration":"2025-04-14T18:35:58.888847176"},{"key":"85319427-4603-4a16-af33-2e9525dda8c0","username":"phil","expiration":"2025-04-14T00:39:34.952183453"},{"key":"f14f187c-355f-444a-88bf-42202f82f947","username":"phil","expiration":"2025-04-17T09:07:05.5541299"},{"key":"004f9f22-2b7e-4448-9c37-7437de47f1e0","username":"phil","expiration":"2025-04-17T08:27:28.3862592"},{"key":"24e8cf17-ad76-45b1-bfb6-79a790035231","username":"admin","expiration":"2025-04-17T08:17:27.7488131"},{"key":"f1d6a110-4232-4ef3-b6ec-9a2962664158","username":"phil","expiration":"2025-04-14T17:23:40.834526839"},{"key":"e48872fa-b89f-494a-b681-11a809d32ff4","username":"phil","expiration":"2025-04-14T17:20:23.265745224"},{"key":"31fcbc15-9902-41d2-8d6f-5b0e40ebddd2","username":"phil","expiration":"2025-04-14T16:45:41.082560826"},{"key":"98c11c42-4e7c-4601-b591-a4af1a5163f9","username":"phil","expiration":"2025-04-17T08:54:04.5078852"},{"key":"27583604-609f-4dac-bb88-01c6035c4142","username":"phil","expiration":"2025-04-17T11:15:56.8479454"},{"key":"967de418-4f86-44ac-b364-eb3a1653aa7d","username":"phil","expiration":"2025-04-17T10:54:19.237888"},{"key":"f20b64c5-a7ed-48c0-a813-6d9802cf9109","username":"phil","expiration":"2025-04-17T08:41:02.6531587"},{"key":"10ea39c2-3869-47da-8630-87b21a88681d","username":"phil","expiration":"2025-04-17T08:45:33.1781528"},{"key":"92d0d6cb-ceb0-4f84-bab3-d959dfb5df9d","username":"phil","expiration":"2025-04-17T08:30:04.2567034"},{"key":"7a7d2e8b-4242-4c18-b7dd-d0565ab5c725","username":"phil","expiration":"2025-04-17T10:10:44.3380846"},{"key":"e14f8ee5-5780-4b9b-bf34-7a41c2bbfcb4","username":"phil","expiration":"2025-04-05T13:46:10.90733016"},{"key":"e1905fc6-ca86-43d7-b1e7-60d458c75a04","username":"phil","expiration":"2025-04-17T09:12:05.990995"},{"key":"a1417644-2b43-4a18-bf5a-26bf3b7ac1fc","username":"phil","expiration":"2025-04-17T10:23:26.122743"},{"key":"7e312d55-bf3e-4dc3-b44a-74d8be591287","username":"phil","expiration":"2025-04-17T10:56:38.9103348"},{"key":"af38add5-b100-4b96-9ffb-5afaccd59979","username":"adf","expiration":"2025-04-14T18:18:47.670506361"},{"key":"4ae3922b-7d5d-45c2-83eb-f8c77e3ce218","username":"phil","expiration":"2025-04-17T08:18:53.5158112"},{"key":"0eb494c8-2fa4-4ef4-915d-4b9fcb3c75ef","username":"phil","expiration":"2025-04-17T08:24:20.412242"},{"key":"0d81e2b4-d9f2-4bc0-b93f-084c086e3707","username":"phil","expiration":"2025-04-17T08:52:41.0723771"},{"key":"04900729-cdcf-4758-9c6e-4f70f03ddb86","username":"phil","expiration":"2025-04-17T11:15:05.0632779"},{"key":"0c8da4b1-bca8-42df-a5b6-65023eadab05","username":"phil","expiration":"2025-04-17T08:15:37.7698648"},{"key":"a3703b55-c7fd-4c5a-9b6a-160c1906aa1a","username":"phil","expiration":"2025-04-17T11:35:39.9101516"},{"key":"ba426671-db67-4d2f-a701-3aa0f284f497","username":"phil","expiration":"2025-04-17T08:54:43.230802"},{"key":"639ea3fa-854a-4052-b1cf-80ea1d3d5917","username":"phil","expiration":"2025-04-17T09:38:47.9716959"},{"key":"a1825159-2e62-48a3-beba-74a3daaca5b5","username":"phil","expiration":"2025-04-17T08:12:28.2166599"},{"key":"799ed420-e843-4777-8c1a-c6d061cca773","username":"phil","expiration":"2025-04-17T08:39:38.8493536"},{"key":"cdd4df80-8139-4479-86ec-953190796d7b","username":"phil","expiration":"2025-04-17T10:02:07.6745458"},{"key":"3d3fb646-9954-496b-98bf-73136c7792ea","username":"phil","expiration":"2025-04-17T10:04:11.7737565"},{"key":"da61796e-402a-4a80-88ae-7607a37989a4","username":"phil","expiration":"2025-04-14T17:07:10.618039573"},{"key":"3beac724-e9f9-4969-9634-60826ad9db43","username":"phil","expiration":"2025-04-17T11:20:36.8424562"},{"key":"0d6e6910-b5dc-45ae-ba8e-c28604939f83","username":"phil","expiration":"2025-04-17T11:19:21.9682553"},{"key":"b7b403a0-d81f-4f7d-bd28-ccf3e2ac706b","username":"phil","expiration":"2025-04-17T10:28:53.9964744"},{"key":"125a4847-3a1c-4834-961f-7f96e997f92e","username":"sowgro","expiration":"2025-04-14T18:35:38.922687686"},{"key":"ce4accc3-0fa8-40fc-a480-1290d12caaed","username":"phil","expiration":"2025-04-17T09:12:58.9504986"},{"key":"adc5ce2f-5a40-4ee5-b9a6-6154860e6861","username":"phil","expiration":"2025-04-17T08:28:48.4584821"},{"key":"2aeaab28-99c9-4b45-bdef-82096c70945e","username":"phil","expiration":"2025-04-14T17:19:48.552121268"},{"key":"7805de89-9b38-46dc-8f59-09c06ef9d2dd","username":"phil","expiration":"2025-04-17T08:52:44.6283659"},{"key":"ad6d92d4-c496-407c-823a-edaa386e67ed","username":"phil","expiration":"2025-04-14T17:07:36.032623002"},{"key":"fbdf7ac2-cf01-4dad-baec-ed9310a4eba7","username":"phil","expiration":"2025-04-17T08:38:12.8162926"},{"key":"7907ccb2-bf01-4962-a280-ebb6aa9c5b20","username":"phil","expiration":"2025-04-17T09:45:01.6777324"},{"key":"a69796dd-734c-4545-ac91-e5fe0387d0ad","username":"phil","expiration":"2025-04-17T08:29:32.671782"},{"key":"9f3e380d-fead-4d40-a1c0-278e857dd674","username":"phil","expiration":"2025-04-17T08:52:29.4148814"},{"key":"efc531fb-ab24-4d5a-a2f5-7f4ede74819f","username":"phil","expiration":"2025-04-13T19:41:51.017327545"},{"key":"88b539a9-3986-41b4-a6ed-a79672042ccf","username":"phil","expiration":"2025-04-17T08:23:45.5712888"},{"key":"68bc53af-e21a-4364-adf5-8f163f642235","username":"phil","expiration":"2025-04-17T08:24:43.3201656"},{"key":"a07ae51f-f80b-4001-95f1-48c11d4917a4","username":"phil","expiration":"2025-04-05T15:04:30.900359001"},{"key":"cc49c007-fd36-4828-b8fa-f5b85ad0676d","username":"phil","expiration":"2025-04-14T16:46:12.80566798"},{"key":"fb2d7dd5-783e-47d8-9a43-8b7693c5f070","username":"phil","expiration":"2025-04-17T11:13:05.9489267"},{"key":"49a0ad40-3223-4f62-94e8-0f9e96241a85","username":"phil","expiration":"2025-04-17T09:13:39.9633546"},{"key":"db53acf2-61d1-45ea-9d01-5c710b80bdaf","username":"phil","expiration":"2025-04-17T08:53:47.487545"},{"key":"d7cef571-0f76-49fe-941f-ecbeae69557a","username":"phil","expiration":"2025-04-05T15:14:00.363201102"},{"key":"3fc557b6-0306-4779-9b74-b7292a5cf1cc","username":"phil","expiration":"2025-04-14T16:06:08.564069822"},{"key":"77392d17-6e0c-45ec-857d-6595a55ddd97","username":"phil","expiration":"2025-04-14T16:06:48.335542315"},{"key":"58e4e2a2-3a36-4fd6-8bb1-ad0831664d01","username":"phil","expiration":"2025-04-12T23:17:42.638952959"},{"key":"6083ae1d-a761-4ed3-8c48-a429afa2c521","username":"phil","expiration":"2025-04-17T08:32:25.3500545"},{"key":"1c438301-e8f5-4c12-a40b-e20b8a282814","username":"keshey","expiration":"2025-04-17T10:26:04.7690496"},{"key":"fe2146e2-7982-4226-b215-96879939f485","username":"phil","expiration":"2025-04-17T08:34:59.3412483"},{"key":"6d2ea170-50aa-4c48-9247-9310a29ae753","username":"phil","expiration":"2025-04-17T11:29:10.0637357"},{"key":"f5f53053-ef5e-4850-93a0-3dc20646f78b","username":"sowgro","expiration":"2025-04-14T18:11:29.438554549"},{"key":"03424ad1-376c-47aa-8553-7f2ea8099d45","username":"phil","expiration":"2025-04-17T08:05:35.7264696"},{"key":"568e4738-70b5-4be7-bfa6-1367cd22ce3f","username":"admin","expiration":"2025-04-17T08:16:59.0542222"},{"key":"7a634e0a-628b-4b31-8950-dc33d4ee5d95","username":"phil","expiration":"2025-04-17T08:15:47.1663258"},{"key":"9c6a36b8-7f71-4b09-b26b-682f1f0be4cb","username":"phil","expiration":"2025-04-17T09:12:15.5425885"},{"key":"f6951471-2578-4a6a-b3e5-b7e97ed9207a","username":"phil","expiration":"2025-04-17T10:23:45.9450825"},{"key":"cb658550-aafc-4a45-89be-f7899e44d7ba","username":"phil","expiration":"2025-04-17T10:50:40.0249774"}] \ No newline at end of file
diff --git a/ufund-api/data/users.json b/ufund-api/data/users.json
index 8543a55..5b2457f 100644
--- a/ufund-api/data/users.json
+++ b/ufund-api/data/users.json
@@ -1,14 +1 @@
-[
- {
- "username": "phil",
- "passwordHash": -1054080181,
- "basket": [],
- "type": "HELPER"
- },
- {
- "username": "admin",
- "passwordHash": 92668751,
- "basket": [],
- "type": "MANAGER"
- }
-] \ No newline at end of file
+[{"username":"keshey","passwordHash":-1134843357,"basket":[],"type":"HELPER"},{"username":"admin","passwordHash":92668751,"basket":[],"type":"MANAGER"},{"username":"phil","passwordHash":-1054080181,"basket":[22,26],"type":"HELPER"}] \ No newline at end of file
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);
}
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
index 94f93cb..6ef6710 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
@@ -39,7 +39,7 @@ public class CupboardControllerTest {
Map<String, Object> needMap = Map.ofEntries(
entry("name", "Test"),
- entry("maxGoal", 100),
+ entry("maxGoal", 100.0),
entry("type", "MONETARY")
);
@@ -55,12 +55,12 @@ public class CupboardControllerTest {
Map<String, Object> needMap = Map.ofEntries(
entry("name", "Name"),
- entry("maxGoal", -100),
+ entry("maxGoal", -100.0),
entry("type", "MONETARY"));
var res = cupboardController.createNeed(needMap);
- assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, res.getStatusCode());
+ assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode());
}
@Test
@@ -69,7 +69,7 @@ public class CupboardControllerTest {
Map<String, Object> needMap = Map.ofEntries(
entry("name", "Name"),
- entry("maxGoal", 100),
+ entry("maxGoal", 100.0),
entry("type", "MONETARY"));
var res = cupboardController.createNeed(needMap);
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java
index b6367ad..cc7df40 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java
@@ -1,7 +1,6 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
-import java.security.InvalidParameterException;
import java.util.Map;
import static java.util.Map.entry;
@@ -239,6 +238,7 @@ public class UserControllerTest {
// When updateUser is called on the Mock User service, throw a Invalid Parameter exception
// exception
doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key);
+
// Invoke
ResponseEntity<User> response = userController.updateUser(user, username, key);
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java
index 54aa4d1..55b7f07 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java
@@ -1,14 +1,14 @@
package com.ufund.api.ufundapi.model;
-import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.*;
-import static org.mockito.Mockito.mock;
-
import java.io.IOException;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.ufund.api.ufundapi.service.CupboardService;
@@ -59,7 +59,7 @@ public class UserTest {
user.addToBasket(need);
- Need getNeed = cupboardService.getNeed(user.getBasketNeeds()[0]);
+ Need getNeed = cupboardService.getNeed(user.getNeeds()[0]);
assertEquals(needs[0], getNeed);
@@ -77,10 +77,10 @@ public class UserTest {
when(cupboardService.getNeed(0)).thenReturn(need2);
user.addToBasket(need);
- user.removeBasketNeed(need);
+ user.removeBasketNeed(need.getId());
user.addToBasket(need2);
- Need getNeed = cupboardService.getNeed(user.getBasketNeeds()[0]);
+ Need getNeed = cupboardService.getNeed(user.getNeeds()[0]);
assertEquals(need2, getNeed);
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
index 0a0cb71..e57c5a3 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
@@ -19,12 +19,14 @@ public class UserServiceTest {
private UserService userService;
private UserDAO mockUserDAO;
+ private CupboardService mockCupboardService;
@BeforeEach
public void setupUserService() {
mockUserDAO = mock(UserDAO.class);
- userService = new UserService(mockUserDAO);
+ mockCupboardService = mock(CupboardService.class);
+ userService = new UserService(mockUserDAO, mockCupboardService);
}
@Test
diff --git a/ufund-ui/src/app/components/cupboard/cupboard.component.ts b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
index 9574de3..24b3e2d 100644
--- a/ufund-ui/src/app/components/cupboard/cupboard.component.ts
+++ b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
@@ -4,13 +4,13 @@ import { UsersService } from '../../services/users.service';
import { Need, GoalType } from '../../models/Need';
import { userType } from '../../models/User';
import { BehaviorSubject, catchError, of } from 'rxjs';
-import {NeedListComponent} from '../need-list/need-list.component';
+import { NeedListComponent } from '../need-list/need-list.component';
@Component({
- selector: 'app-cupboard',
- standalone: false,
- templateUrl: './cupboard.component.html',
- styleUrl: './cupboard.component.css'
+ selector: 'app-cupboard',
+ standalone: false,
+ templateUrl: './cupboard.component.html',
+ styleUrl: './cupboard.component.css'
})
export class CupboardComponent implements OnInit {
@@ -23,140 +23,152 @@ export class CupboardComponent implements OnInit {
constructor(private cupboardService: CupboardService, private usersService: UsersService) { }
ngOnInit(): void {
- this.cupboardService.getNeeds().subscribe(n => this.needs = n);
- this.close();
- this.openmenu();
-
- if (this.isManager()) {
- console.log("Admin view of Cupboard");
- } else {
- console.log("Limited helper view of Cupboard");
- }
+ this.cupboardService.getNeeds().subscribe(n => this.needs = n);
+ this.close();
+ this.openmenu();
+
+ if (this.isManager()) {
+ console.log("Admin view of Cupboard");
+ } else {
+ console.log("Limited helper view of Cupboard");
+ }
}
selectedNeed: any = {
- name: '',
- id: null,
- maxGoal: null,
- type: ''
+ name: '',
+ id: null,
+ maxGoal: null,
+ type: ''
};
selectedNeedId: number | null = null;
private hideElement(element: any) {
- if (element){
- element.style.visibility = 'hidden';
- element.style.position = 'absolute';
- }
+ if (element) {
+ element.style.visibility = 'hidden';
+ element.style.position = 'absolute';
+ }
}
private showElement(element: any) {
- if (element){
- element.style.visibility = 'visible';
- element.style.position = 'relative';
- }
+ if (element) {
+ element.style.visibility = 'visible';
+ element.style.position = 'relative';
+ }
}
openmenu() {
- const menuElement = document.getElementById('menu');
- this.showElement(menuElement);
+ const menuElement = document.getElementById('menu');
+ this.showElement(menuElement);
}
opencreate() {
- this.close();
- this.showElement(document.getElementById('create-form'));
+ this.close();
+ this.showElement(document.getElementById('create-form'));
}
openupdate() {
- this.close();
- this.showElement(document.getElementById('update-form'));
+ this.close();
+ this.showElement(document.getElementById('update-form'));
}
back() {
- this.close();
- this.openmenu();
+ this.close();
+ this.openmenu();
}
close() {
- this.hideElement(document.getElementById('create-form'));
- this.hideElement(document.getElementById('destroy-form'));
- this.hideElement(document.getElementById('menu'));
- this.hideElement(document.getElementById('update-form'));
+ this.hideElement(document.getElementById('create-form'));
+ this.hideElement(document.getElementById('destroy-form'));
+ this.hideElement(document.getElementById('menu'));
+ this.hideElement(document.getElementById('update-form'));
}
populateForm(need: any): void {
- this.selectedNeed = { ...need };
+ this.selectedNeed = { ...need };
}
isManager() {
- const type = this.usersService.getCurrentUser()?.type;
- return type === ("MANAGER" as unknown as userType);
+ const type = this.usersService.getCurrentUser()?.type;
+ return type === ("MANAGER" as unknown as userType);
}
update(form: any) {
- console.log(form);
- const need: Need = {
- name: form.name,
- id: form.id, //system will control this
- maxGoal: form.maxGoal,
- type: GoalType[form.type as keyof typeof GoalType],
- filterAttributes: [],
- current: 0
- };
- console.log("need:", need);
- console.log(need.id, need, "need updated");
- this.cupboardService.updateNeed(need.id, need)
- .pipe(catchError((ex, r) => {
- this.statusText.next("Max goal must be greater than 0 " + friendlyHttpStatus[ex.status])
- return of()
- }))
- .subscribe(
- (result) => {
- if (result) {
- console.log("need updated successfully");
- this.needList?.refresh()
- } else {
- console.log("need update failed");
- }
- }
-
- );
+ console.log(form);
+ const need: Need = {
+ name: form.name,
+ id: form.id, //system will control this
+ maxGoal: form.maxGoal,
+ type: GoalType[form.type as keyof typeof GoalType],
+ filterAttributes: [],
+ current: 0
+ };
+ console.log("need:", need);
+ console.log(need.id, need, "need updated");
+ this.cupboardService.updateNeed(need.id, need)
+ .pipe(catchError((ex, r) => {
+ if (ex.status == 500) {
+ this.statusText.next("Fields cannot be blank");
+ } else if (ex.status == 400) {
+ this.statusText.next("Goal must be greater than 0");
+ } else {
+ this.statusText.next("Error on creating need");
+ }
+ return of()
+ }))
+ .subscribe(
+ (result) => {
+ if (result) {
+ console.log("need updated successfully");
+ this.needList?.refresh()
+ } else {
+ console.log("need update failed");
+ }
+ }
+
+ );
}
submit(form: any) {
- const need: Need = {
- name: form.name,
- id: 0,
- maxGoal: form.maxGoal,
- type: form.type,
- filterAttributes: [],
- current: 0
- };
- console.log("need:", need);
- console.log("form submitted. creating need: ", need);
- this.cupboardService.createNeed(need)
- .pipe(catchError((ex, r) => {
- this.statusText.next("Max goal must be greater than 0 " + friendlyHttpStatus[ex.status])
- return of()
- }))
- .subscribe(
- (result) => {
- if (result) {
- console.log("need created successfully");
- this.needList?.refresh()
- } else {
- console.log("need creation failed");
- }
- }
-
- );
+ const need: Need = {
+ name: form.name,
+ id: 0,
+ maxGoal: form.maxGoal,
+ type: form.type,
+ filterAttributes: [],
+ current: 0
+ };
+ console.log("need:", need);
+ console.log("form submitted. creating need: ", need);
+ this.cupboardService.createNeed(need)
+ .pipe(catchError((ex, r) => {
+ if (ex.status == 500) {
+ this.statusText.next("Fields cannot be blank");
+ } else if (ex.status == 400) {
+ this.statusText.next("Goal must be greater than 0");
+ } else {
+ this.statusText.next("Error on creating need");
+ }
+ return of()
+ }))
+ .subscribe(
+ (result) => {
+ if (result) {
+ console.log("need created successfully");
+ this.needList?.refresh()
+ } else {
+ console.log("need creation failed");
+ }
+ }
+
+ );
}
destroy() {
}
- }
+}
-let friendlyHttpStatus: {[key: number]: string} = {
+let friendlyHttpStatus: { [key: number]: string } = {
200: 'OK',
201: 'Created',
202: 'Accepted',
@@ -191,6 +203,7 @@ let friendlyHttpStatus: {[key: number]: string} = {
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
418: 'I\'m a teapot',
+ 422: 'Unprocessable Entity',
429: 'Too Many Requests',
500: 'Internal Server Error',
501: 'Not Implemented',
diff --git a/ufund-ui/src/app/components/dashboard/dashboard.component.html b/ufund-ui/src/app/components/dashboard/dashboard.component.html
index fc8baf0..a1151b7 100644
--- a/ufund-ui/src/app/components/dashboard/dashboard.component.html
+++ b/ufund-ui/src/app/components/dashboard/dashboard.component.html
@@ -1,5 +1,4 @@
<h1>Dashboard</h1>
-<input type="button" value="Back to Login" (click)="back()">
<app-cupboard></app-cupboard>
-<app-funding-basket></app-funding-basket> \ No newline at end of file
+<app-funding-basket *ngIf="!isManager()"></app-funding-basket> \ No newline at end of file
diff --git a/ufund-ui/src/app/components/dashboard/dashboard.component.ts b/ufund-ui/src/app/components/dashboard/dashboard.component.ts
index 48c5894..b9faefa 100644
--- a/ufund-ui/src/app/components/dashboard/dashboard.component.ts
+++ b/ufund-ui/src/app/components/dashboard/dashboard.component.ts
@@ -1,4 +1,6 @@
import { Component } from '@angular/core';
+import { UsersService } from '../../services/users.service';
+import { userType } from '../../models/User';
@Component({
selector: 'app-dashboard',
@@ -7,9 +9,13 @@ import { Component } from '@angular/core';
styleUrl: './dashboard.component.css'
})
export class DashboardComponent {
- constructor() {}
+ constructor(
+ protected usersService: UsersService,
+ ) {}
+
+ isManager() {
+ const type = this.usersService.getCurrentUser()?.type;
+ return type === ("MANAGER" as unknown as userType);
+ }
- back() {
- window.history.back();
- }
}
diff --git a/ufund-ui/src/app/components/funding-basket/funding-basket.component.css b/ufund-ui/src/app/components/funding-basket/funding-basket.component.css
index e69de29..3dec496 100644
--- a/ufund-ui/src/app/components/funding-basket/funding-basket.component.css
+++ b/ufund-ui/src/app/components/funding-basket/funding-basket.component.css
@@ -0,0 +1,7 @@
+td, p {
+ border: 2px solid #000;
+ border-radius: 5px;
+ padding: 5px;
+ margin: 5px;
+
+} \ No newline at end of file
diff --git a/ufund-ui/src/app/components/funding-basket/funding-basket.component.html b/ufund-ui/src/app/components/funding-basket/funding-basket.component.html
index 0a880af..504e694 100644
--- a/ufund-ui/src/app/components/funding-basket/funding-basket.component.html
+++ b/ufund-ui/src/app/components/funding-basket/funding-basket.component.html
@@ -2,33 +2,30 @@
<div id="needCount">
<label for="needCount">Needs in Basket:</label>
- <span>{{ needCount }}</span>
+ <span>{{ this.usersService.getBasket().getValue().length }}</span>
</div>
-<div *ngIf="isBasketEmpty">
+<div *ngIf="this.usersService.getBasket().getValue().length == 0">
<h2>There are no needs in the basket</h2>
</div>
-<table class="needs" *ngIf="!isBasketEmpty">
+<table class="needs" id="funding-basket" *ngIf="this.usersService.getBasket().getValue().length != 0">
<thead>
<tr>
<th class="need"></th>
</tr>
</thead>
<tbody>
- <tr *ngFor="let need of basket">
+ <tr *ngFor="let need of usersService.getBasket().getValue()">
<td>
- <a routerLink="/need/{{need.id}}">
- {{need.name}}
- </a>
- <div>
- <button type="button" class="addNeed" title="add need"
- (click)="addNeed(need)">Add Need</button>
- </div>
+ <a routerLink="/need/{{need.id}}">{{need.name}}</a>
+ <p>Goal: {{need.maxGoal}}</p>
+ <p>Current: {{(need.current).toFixed(2)}}</p>
+ <p>How much to Contribute: <input type="number" placeholder="insert value" min="1" id={{need.id}} class="contribution"></p>
<br>
<div>
- <button type="button" class="removeNeed" title="delete need"
- (click)="removeNeed(need)">Remove Need</button>
+ <button type="button" class="removeNeed" title="delete need"
+ (click)="this.usersService.removeNeed(need.id)">Remove Need</button>
</div>
</td>
</tr>
@@ -36,6 +33,7 @@
</table>
<br>
<div>
- <button type="submit" class="checkout" title="checkout"
- (click)="checkout()">Checkout</button>
+ <p *ngIf="!isValid">Invalid input in funding basket!</p>
+ <button type="submit" class="checkout" title="checkout" (click)="checkout()">Checkout</button>
+ <span *ngIf="statusText">{{statusText | async}}</span>
</div> \ No newline at end of file
diff --git a/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts b/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
index dd68c0c..e654711 100644
--- a/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
+++ b/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
@@ -1,10 +1,11 @@
-import {Component, OnInit} from '@angular/core';
+import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {User} from '../../models/User';
import { UsersService } from '../../services/users.service';
import { Need } from '../../models/Need';
import { NeedListComponent } from '../need-list/need-list.component';
import { Router } from '@angular/router';
import { CupboardService } from '../../services/cupboard.service';
+import { BehaviorSubject, catchError, firstValueFrom, Observable } from 'rxjs';
@Component({
selector: 'app-funding-basket',
@@ -12,80 +13,68 @@ import { CupboardService } from '../../services/cupboard.service';
templateUrl: './funding-basket.component.html',
styleUrl: './funding-basket.component.css'
})
-export class FundingBasketComponent implements
- OnInit {
- user!: User;
- needs: Need[] = [];
- basket: Need[] = [];
- needCount = 0;
- need_quantity: {[key: number]: number} = {};
+export class FundingBasketComponent implements OnInit {
+ statusText: any;
constructor(
private router: Router,
- private cupboardService: CupboardService, private usersService: UsersService
+ protected cupboardService: CupboardService,
+ protected usersService: UsersService
) {}
+ @ViewChild("contribution") contribution?: Input;
+ @Input() isValid: boolean = true;
+
+ // this is for login rerouting
ngOnInit(): void {
if (!this.usersService.getCurrentUser()) {
this.router.navigate(['/login'], {queryParams: {redir: this.router.url}});
return;
}
- this.cupboardService.getNeeds().subscribe(n => this.needs = n)
- const currentUser = this.usersService.getCurrentUser();
- if (currentUser) {
- this.user = currentUser;
- }
- this.getBasketNeeds();
- }
- getBasketNeeds(): void {
- if (this.user && this.user.basket) {
- this.user.basket.forEach(need => {
- // if (this.isInBasket(need)) {
- // this.basket.push(need);
- // }
- });
- }
- }
-
- isInBasket(need: Need): boolean {
- return this.basket.includes(need)
- }
+ this.usersService.refreshBasket();
+ // this.usersService.removeNeed(); <- call this to remove
+ }
- addNeed(need: Need, quantity: number = 1): void {
- if (this.user && !this.isInBasket(need)) {
- this.basket.push(need);
- this.need_quantity[need.id] = quantity;
- }
- if (this.isInBasket(need)) {
- this.need_quantity[need.id] += quantity;
- }
- this.needCount++;
+ async checkout() {
+ this.isValid = true;
+ for (let c of document.getElementById("funding-basket")?.querySelectorAll('.contribution')!) {
+ let contribution = c as HTMLInputElement;
+ contribution.setAttribute("style","");
+ if ( contribution.value == '' || contribution.valueAsNumber <= 0) {
+ this.isValid = false;
+ contribution.setAttribute("style","color: #ff0000");
+ }
}
-
- removeNeed(need: Need, quantity: number = 1): void {
- if (this.user && this.isInBasket(need)) {
- this.need_quantity[need.id] -= quantity;
- if (this.need_quantity[need.id] === 0) {
- this.basket = this.basket.filter(n => need.id !== n.id);
- }
- this.needCount--;
- }
+ if (this.isValid) {
+ for (let c of document.getElementById("funding-basket")?.querySelectorAll('.contribution')!) {
+ let contribution = c as HTMLInputElement;
+ let need = await firstValueFrom(this.cupboardService.getNeed(+contribution.id));
+ need.current +=+ contribution.value;
+ this.usersService.removeNeed(+need.id);
+ this.cupboardService.updateNeed(need.id, need)
+ .pipe(catchError((ex, r) => {
+ if (ex.status == 500) {
+ this.statusText.next('Fields cannot be blank');
+ } else if (ex.status == 400) {
+ this.statusText.next('Goal must be greater than 0');
+ } else {
+ this.statusText.next('Error on creating need');
+ }
+ return new Observable<string>();
+ }))
+ .subscribe((result) => {
+ if (result) {
+ console.log('need updated successfully');
+ //this.needList?.refresh()
+ } else {
+ console.log('need update failed');
+ }
+ });
+ }
}
+ }
- removeAllNeeds(): void {
- this.basket.forEach(need => {
- this.need_quantity = [];
- });
- this.basket = [];
- this.needCount = 0;
- }
- isBasketEmpty(): boolean {
- return this.needCount === 0;
- }
- checkout(): void {
- this.removeAllNeeds();
- }
}
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.html b/ufund-ui/src/app/components/need-list/need-list.component.html
index 4a67dfa..36c12d0 100644
--- a/ufund-ui/src/app/components/need-list/need-list.component.html
+++ b/ufund-ui/src/app/components/need-list/need-list.component.html
@@ -24,5 +24,5 @@
{{need.name}}
</a>
<button (click)="delete(need.id)" *ngIf="isManager()">Delete</button>
- <!-- <button (click)="add(need)" *ngIf="isHelper()">Add To Basket</button> -->
+ <button (click)="add(need)" *ngIf="isHelper()">Add To Basket</button>
</li>
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.ts b/ufund-ui/src/app/components/need-list/need-list.component.ts
index e1c0de1..25f05d6 100644
--- a/ufund-ui/src/app/components/need-list/need-list.component.ts
+++ b/ufund-ui/src/app/components/need-list/need-list.component.ts
@@ -105,19 +105,22 @@ export class NeedListComponent {
return type === ("HELPER" as unknown as userType);
}
- add(need: number) {
+ add(need: Need) {
const currentUser = this.usersService.getCurrentUser();
+ //console.log("get current user in angular:", currentUser)
if (currentUser) {
- this.usersService.updateUser(currentUser.username, currentUser).subscribe(() => {
- const currentUser = this.usersService.getCurrentUser();
- if (currentUser && currentUser.basket) {
- currentUser.basket.push(need);
- console.log("added to basket");
- }
- error: (err: any) => {
- console.error(err);
- }
- });
+ if (!currentUser.basket.includes(need.id)) {
+ currentUser.basket.push(need.id);
+ this.usersService.updateUser(currentUser).subscribe(() => {
+ this.usersService.refreshBasket();
+ error: (err: any) => {
+ console.error(err);
+ }
+ });
+ } else {
+ window.alert("This need is already in your basket!")
+ }
+
}
diff --git a/ufund-ui/src/app/components/need-page/need-page.component.html b/ufund-ui/src/app/components/need-page/need-page.component.html
index 8234ac7..90fd459 100644
--- a/ufund-ui/src/app/components/need-page/need-page.component.html
+++ b/ufund-ui/src/app/components/need-page/need-page.component.html
@@ -1,3 +1,4 @@
+<button routerLink="/dashboard">Back to dashboard</button>
<h1>Viewing Need: {{need?.name}}</h1>
<a>internal id: {{need?.id}}</a>
<div style="display: flex; column-gap: 6px;">
@@ -18,4 +19,4 @@
<p>Goal: {{need?.maxGoal}}</p>
<p>Current: {{need?.current}}</p>
-<p>This goal is <strong>{{((need?.current ?? 0)*100) / (need?.maxGoal ?? 0)}}%</strong> complete!</p> \ No newline at end of file
+<p>This goal is <strong>{{(((need?.current ?? 0)*100) / (need?.maxGoal ?? 0)).toFixed(0)}}%</strong> complete!</p> \ No newline at end of file
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts
index bc31870..dba8185 100644
--- a/ufund-ui/src/app/services/users.service.ts
+++ b/ufund-ui/src/app/services/users.service.ts
@@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs';
import {User} from '../models/User';
+import { Need } from '../models/Need';
+import { CupboardService } from './cupboard.service';
@Injectable({
providedIn: 'root'
@@ -10,6 +12,7 @@ export class UsersService {
private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null);
private apiKey: string = "";
+ private basket = new BehaviorSubject<Need[]>([]);
private url = "http://localhost:8080/users"
private authUrl = "http://localhost:8080/auth"
@@ -29,7 +32,8 @@ export class UsersService {
};
constructor(
- private http: HttpClient
+ private http: HttpClient,
+ private cupboardService: CupboardService,
) {}
async createUser(username:string, password:string) {
@@ -40,10 +44,8 @@ export class UsersService {
return this.http.get<User>(`${this.url}/${id}`, this.httpOptions)
}
- updateUser(id: string, user: User): Observable<User> {
- console.log(id, user)
- console.log(this.apiKey)
- return this.http.put<User>(`${this.url}/${id}`,user, this.httpOptions)
+ updateUser(user: User): Observable<User> {
+ return this.http.put<User>(`${this.url}/${user.username}`,user, this.httpOptions2)
}
deleteUser(id: number): Observable<boolean> {
@@ -72,4 +74,28 @@ export class UsersService {
this.currentUser.next(currentU);
// this.currentUser.subscribe(r => console.log("currentUser: "+r.username))
}
+
+ refreshBasket() {
+ let promiseArr = this.getCurrentUser()!.basket.map(async needID => {
+ return await firstValueFrom(this.cupboardService.getNeed(needID));
+ })
+ Promise.all(promiseArr).then(r => this.basket.next(r));
+ }
+
+ removeNeed(id: number) {
+ let newArr = this.basket.getValue().filter(v => v.id != id);
+ this.basket.next(newArr);
+ this.getCurrentUser()!.basket = newArr.map(need => need.id);
+ this.updateUser(this.getCurrentUser()!).subscribe(() => {
+ this.refreshBasket();
+ error: (err: any) => {
+ console.error(err);
+ }
+ });
+ }
+
+ getBasket() {
+ return this.basket;
+ }
+
}