From d1a7322bb8b7f99e06fa354c0f6a9ca85c61c41e Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 19:10:58 -0500 Subject: [PATCH 1/8] Create MySQL DB - Created MySQL 'web_shop' server running on port 3306 - Configured spring.datasource user/pass --- .idea/.gitignore | 10 +++++++++ .idea/compiler.xml | 18 +++++++++++++++ .idea/dataSources.xml | 17 ++++++++++++++ .idea/encodings.xml | 6 +++++ .idea/flexpath-unit-2-final.iml | 9 ++++++++ .idea/jarRepositories.xml | 20 +++++++++++++++++ .idea/misc.xml | 14 ++++++++++++ .idea/modules.xml | 8 +++++++ .idea/vcs.xml | 6 +++++ .../.idea/dataSources.local.xml | 11 ++++++++++ java-springboot-final/.idea/dataSources.xml | 19 ++++++++++++++++ .../dataSources/data_sources_history.xml | 22 +++++++++++++++++++ java-springboot-final/.idea/sqldialects.xml | 6 +++++ .../src/main/resources/application.properties | 2 +- 14 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/dataSources.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/flexpath-unit-2-final.iml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 java-springboot-final/.idea/dataSources.local.xml create mode 100644 java-springboot-final/.idea/dataSources.xml create mode 100644 java-springboot-final/.idea/dataSources/data_sources_history.xml create mode 100644 java-springboot-final/.idea/sqldialects.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..84b29a0 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..eac9a25 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..ba609eb --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/flexpath-unit-2-final.iml b/.idea/flexpath-unit-2-final.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/flexpath-unit-2-final.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..859aef5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2177b28 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java-springboot-final/.idea/dataSources.local.xml b/java-springboot-final/.idea/dataSources.local.xml new file mode 100644 index 0000000..76651b3 --- /dev/null +++ b/java-springboot-final/.idea/dataSources.local.xml @@ -0,0 +1,11 @@ + + + + + + master_key + nick0 + + + + \ No newline at end of file diff --git a/java-springboot-final/.idea/dataSources.xml b/java-springboot-final/.idea/dataSources.xml new file mode 100644 index 0000000..0a23f24 --- /dev/null +++ b/java-springboot-final/.idea/dataSources.xml @@ -0,0 +1,19 @@ + + + + + mysql.8 + true + true + $PROJECT_DIR$/src/main/resources/application.properties + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/web_shop + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/java-springboot-final/.idea/dataSources/data_sources_history.xml b/java-springboot-final/.idea/dataSources/data_sources_history.xml new file mode 100644 index 0000000..c24d484 --- /dev/null +++ b/java-springboot-final/.idea/dataSources/data_sources_history.xml @@ -0,0 +1,22 @@ + + + + + mysql.8 + true + true + C:/Users/nicho/OneDrive/Documents/GitHub/flexpath-unit-2-final/java-springboot-final/src/main/resources/application.properties + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/web_shop + + + + + + master_key + nick0 + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/java-springboot-final/.idea/sqldialects.xml b/java-springboot-final/.idea/sqldialects.xml new file mode 100644 index 0000000..56782ca --- /dev/null +++ b/java-springboot-final/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java-springboot-final/src/main/resources/application.properties b/java-springboot-final/src/main/resources/application.properties index 6a7d9e5..d9dba17 100644 --- a/java-springboot-final/src/main/resources/application.properties +++ b/java-springboot-final/src/main/resources/application.properties @@ -1,3 +1,3 @@ spring.datasource.url=jdbc:mysql://localhost:3306/web_shop spring.datasource.username=root -spring.datasource.password=your-password +spring.datasource.password=Nb221291?? From 4d9b82261d7536bae398354e6a41270464ea10e4 Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 19:42:30 -0500 Subject: [PATCH 2/8] All set up for Exercises! 1. Loaded/ran 'create-database.sql' script in My SQL workbench successfully serving over port 3306 2. Test ran Spring Boor Application in IntelliJ 3. Postman collections loaded from import into workspace 3. Ran POST login to authenticate as Admin 4. Ran Profile and User REST endpoint requests with success. using Postman *Read for exercises* --- .../postman/.postman/resources.yaml | 11 +++++++++++ .../web-store/.resources/definition.yaml | 11 +++++++++++ .../Order Items/.resources/definition.yaml | 2 ++ .../Create Order Item 1.request.yaml | 12 ++++++++++++ .../Delete Order Item 1.request.yaml | 4 ++++ .../Get All Order Items.request.yaml | 4 ++++ .../Get Order Item By Id.request.yaml | 4 ++++ .../Update Order Item 1.request.yaml | 12 ++++++++++++ .../web-store/Orders/.resources/definition.yaml | 2 ++ .../Orders/Create Order 6.request.yaml | 10 ++++++++++ .../Orders/Delete Order 6.request.yaml | 4 ++++ .../Orders/Get All Orders.request.yaml | 4 ++++ .../Orders/Get Order By Id.request.yaml | 4 ++++ .../Orders/Update Order 6.request.yaml | 10 ++++++++++ .../Products/.resources/definition.yaml | 2 ++ .../Products/Create Product 6.request.yaml | 11 +++++++++++ .../Products/Delete Product 6.request.yaml | 4 ++++ .../Products/Get All Products.request.yaml | 4 ++++ .../Products/Get Product By Id.request.yaml | 4 ++++ .../Products/Update Product 6.request.yaml | 11 +++++++++++ .../Profile/.resources/definition.yaml | 2 ++ .../Change Password To -admin-.request.yaml | 12 ++++++++++++ .../Change Password To -test-.request.yaml | 12 ++++++++++++ .../Profile/Get Profile Roles.request.yaml | 4 ++++ .../web-store/Profile/Get Profile.request.yaml | 4 ++++ .../web-store/Users/.resources/definition.yaml | 2 ++ .../Users/Add ADMIN role to test.request.yaml | 7 +++++++ .../Users/Add TEST role to test.request.yaml | 7 +++++++ .../Users/Change Test Password.request.yaml | 7 +++++++ .../Users/Create Test User.request.yaml | 11 +++++++++++ .../Delete TEST role from test.request.yaml | 7 +++++++ .../Users/Delete Test User.request.yaml | 4 ++++ .../web-store/Users/Get All Users.request.yaml | 4 ++++ .../Users/Get Test User Roles.request.yaml | 4 ++++ .../web-store/Users/Get Test User.request.yaml | 4 ++++ .../collections/web-store/login.request.yaml | 17 +++++++++++++++++ .../postman/globals/workspace.globals.yaml | 2 ++ 37 files changed, 240 insertions(+) create mode 100644 java-springboot-final/postman/.postman/resources.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/.resources/definition.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Order Items/.resources/definition.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Order Items/Create Order Item 1.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Order Items/Delete Order Item 1.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Order Items/Get All Order Items.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Order Items/Get Order Item By Id.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Order Items/Update Order Item 1.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Orders/.resources/definition.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Orders/Create Order 6.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Orders/Delete Order 6.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Orders/Get All Orders.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Orders/Get Order By Id.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Orders/Update Order 6.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Products/.resources/definition.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Products/Create Product 6.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Products/Delete Product 6.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Products/Get All Products.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Products/Get Product By Id.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Products/Update Product 6.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Profile/.resources/definition.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -admin-.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -test-.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile Roles.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/.resources/definition.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Add ADMIN role to test.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Add TEST role to test.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Change Test Password.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Create Test User.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Delete TEST role from test.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Delete Test User.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Get All Users.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Get Test User Roles.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/Users/Get Test User.request.yaml create mode 100644 java-springboot-final/postman/postman/collections/web-store/login.request.yaml create mode 100644 java-springboot-final/postman/postman/globals/workspace.globals.yaml diff --git a/java-springboot-final/postman/.postman/resources.yaml b/java-springboot-final/postman/.postman/resources.yaml new file mode 100644 index 0000000..33ff8ff --- /dev/null +++ b/java-springboot-final/postman/.postman/resources.yaml @@ -0,0 +1,11 @@ +# Use this workspace to collaborate +workspace: + id: 3125f098-2d4b-4cee-82d6-fdf6b8cfbf60 + +localResources: + collections: + - ../web-store.postman_collection.json + +cloudResources: + collections: + ../postman/collections/web-store: 7aa67b5d-f01a-499d-908b-d2893184bd9c diff --git a/java-springboot-final/postman/postman/collections/web-store/.resources/definition.yaml b/java-springboot-final/postman/postman/collections/web-store/.resources/definition.yaml new file mode 100644 index 0000000..b2e3d41 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/.resources/definition.yaml @@ -0,0 +1,11 @@ +$kind: collection +description: "" +variables: + token: "" + password: admin +auth: + - id: 0868ff0e-f89f-4f44-9f70-6a5cf6e40fc1 + type: bearer + name: bearer auth + credentials: + token: "{{token}}" diff --git a/java-springboot-final/postman/postman/collections/web-store/Order Items/.resources/definition.yaml b/java-springboot-final/postman/postman/collections/web-store/Order Items/.resources/definition.yaml new file mode 100644 index 0000000..afdeb9a --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Order Items/.resources/definition.yaml @@ -0,0 +1,2 @@ +$kind: collection +order: 5000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Order Items/Create Order Item 1.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Order Items/Create Order Item 1.request.yaml new file mode 100644 index 0000000..f20d8b6 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Order Items/Create Order Item 1.request.yaml @@ -0,0 +1,12 @@ +$kind: http-request +url: http://localhost:8080/api/order-items +method: POST +body: + type: json + content: |- + { + "orderId": 1, + "productId": 1, + "quantity": 1 + } +order: 3000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Order Items/Delete Order Item 1.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Order Items/Delete Order Item 1.request.yaml new file mode 100644 index 0000000..574b1c4 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Order Items/Delete Order Item 1.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/order-items/1 +method: DELETE +order: 5000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Order Items/Get All Order Items.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Order Items/Get All Order Items.request.yaml new file mode 100644 index 0000000..792067b --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Order Items/Get All Order Items.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/order-items +method: GET +order: 1000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Order Items/Get Order Item By Id.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Order Items/Get Order Item By Id.request.yaml new file mode 100644 index 0000000..fe977a2 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Order Items/Get Order Item By Id.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/order-items/1 +method: GET +order: 2000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Order Items/Update Order Item 1.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Order Items/Update Order Item 1.request.yaml new file mode 100644 index 0000000..ca94f1b --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Order Items/Update Order Item 1.request.yaml @@ -0,0 +1,12 @@ +$kind: http-request +url: http://localhost:8080/api/order-items/1 +method: PUT +body: + type: json + content: |- + { + "orderId": 1, + "productId": 1, + "quantity": 2 + } +order: 4000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Orders/.resources/definition.yaml b/java-springboot-final/postman/postman/collections/web-store/Orders/.resources/definition.yaml new file mode 100644 index 0000000..dcd7396 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Orders/.resources/definition.yaml @@ -0,0 +1,2 @@ +$kind: collection +order: 4000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Orders/Create Order 6.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Orders/Create Order 6.request.yaml new file mode 100644 index 0000000..b3962ad --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Orders/Create Order 6.request.yaml @@ -0,0 +1,10 @@ +$kind: http-request +url: http://localhost:8080/api/orders +method: POST +body: + type: json + content: |- + { + "username": "admin" + } +order: 3000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Orders/Delete Order 6.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Orders/Delete Order 6.request.yaml new file mode 100644 index 0000000..9a4b9d3 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Orders/Delete Order 6.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/orders/6 +method: DELETE +order: 5000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Orders/Get All Orders.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Orders/Get All Orders.request.yaml new file mode 100644 index 0000000..917e167 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Orders/Get All Orders.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/orders +method: GET +order: 1000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Orders/Get Order By Id.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Orders/Get Order By Id.request.yaml new file mode 100644 index 0000000..b12fcab --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Orders/Get Order By Id.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/orders/1 +method: GET +order: 2000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Orders/Update Order 6.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Orders/Update Order 6.request.yaml new file mode 100644 index 0000000..bb7fecd --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Orders/Update Order 6.request.yaml @@ -0,0 +1,10 @@ +$kind: http-request +url: http://localhost:8080/api/orders/6 +method: PUT +body: + type: json + content: |- + { + "username" : "test" + } +order: 4000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Products/.resources/definition.yaml b/java-springboot-final/postman/postman/collections/web-store/Products/.resources/definition.yaml new file mode 100644 index 0000000..40bbb1c --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Products/.resources/definition.yaml @@ -0,0 +1,2 @@ +$kind: collection +order: 3000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Products/Create Product 6.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Products/Create Product 6.request.yaml new file mode 100644 index 0000000..9fea8be --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Products/Create Product 6.request.yaml @@ -0,0 +1,11 @@ +$kind: http-request +url: http://localhost:8080/api/products +method: POST +body: + type: json + content: |- + { + "name": "test", + "price": 100 + } +order: 3000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Products/Delete Product 6.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Products/Delete Product 6.request.yaml new file mode 100644 index 0000000..74a143f --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Products/Delete Product 6.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/products/6 +method: DELETE +order: 5000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Products/Get All Products.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Products/Get All Products.request.yaml new file mode 100644 index 0000000..33c1789 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Products/Get All Products.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/products +method: GET +order: 1000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Products/Get Product By Id.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Products/Get Product By Id.request.yaml new file mode 100644 index 0000000..3eeac89 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Products/Get Product By Id.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/products/1 +method: GET +order: 2000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Products/Update Product 6.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Products/Update Product 6.request.yaml new file mode 100644 index 0000000..055fbe2 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Products/Update Product 6.request.yaml @@ -0,0 +1,11 @@ +$kind: http-request +url: http://localhost:8080/api/products/6 +method: PUT +body: + type: json + content: |- + { + "name": "test", + "price": 200 + } +order: 4000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Profile/.resources/definition.yaml b/java-springboot-final/postman/postman/collections/web-store/Profile/.resources/definition.yaml new file mode 100644 index 0000000..53d990d --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Profile/.resources/definition.yaml @@ -0,0 +1,2 @@ +$kind: collection +order: 1000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -admin-.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -admin-.request.yaml new file mode 100644 index 0000000..eac03b4 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -admin-.request.yaml @@ -0,0 +1,12 @@ +$kind: http-request +name: Change Password To "admin" +url: http://localhost:8080/api/profile/change-password +method: PUT +body: + type: json + content: admin +scripts: + - type: afterResponse + code: pm.collectionVariables.set('password', 'admin'); + language: text/javascript +order: 4000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -test-.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -test-.request.yaml new file mode 100644 index 0000000..a906d7c --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Profile/Change Password To -test-.request.yaml @@ -0,0 +1,12 @@ +$kind: http-request +name: Change Password To "test" +url: http://localhost:8080/api/profile/change-password +method: PUT +body: + type: json + content: test +scripts: + - type: afterResponse + code: pm.collectionVariables.set('password', 'test'); + language: text/javascript +order: 3000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile Roles.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile Roles.request.yaml new file mode 100644 index 0000000..f02c928 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile Roles.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/profile/roles +method: GET +order: 2000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile.request.yaml new file mode 100644 index 0000000..d034a7d --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Profile/Get Profile.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/profile +method: GET +order: 1000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/.resources/definition.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/.resources/definition.yaml new file mode 100644 index 0000000..6522e17 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/.resources/definition.yaml @@ -0,0 +1,2 @@ +$kind: collection +order: 2000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Add ADMIN role to test.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Add ADMIN role to test.request.yaml new file mode 100644 index 0000000..135b9fe --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Add ADMIN role to test.request.yaml @@ -0,0 +1,7 @@ +$kind: http-request +url: http://localhost:8080/api/users/test/roles +method: POST +body: + type: json + content: ADMIN +order: 5000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Add TEST role to test.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Add TEST role to test.request.yaml new file mode 100644 index 0000000..f8a1324 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Add TEST role to test.request.yaml @@ -0,0 +1,7 @@ +$kind: http-request +url: http://localhost:8080/api/users/test/roles +method: POST +body: + type: json + content: TEST +order: 6000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Change Test Password.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Change Test Password.request.yaml new file mode 100644 index 0000000..1fca6c8 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Change Test Password.request.yaml @@ -0,0 +1,7 @@ +$kind: http-request +url: http://localhost:8080/api/users/test/password +method: PUT +body: + type: json + content: test +order: 4000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Create Test User.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Create Test User.request.yaml new file mode 100644 index 0000000..8a727e6 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Create Test User.request.yaml @@ -0,0 +1,11 @@ +$kind: http-request +url: http://localhost:8080/api/users +method: POST +body: + type: json + content: |- + { + "username": "test", + "password": "admin" + } +order: 2000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Delete TEST role from test.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Delete TEST role from test.request.yaml new file mode 100644 index 0000000..4fab4cb --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Delete TEST role from test.request.yaml @@ -0,0 +1,7 @@ +$kind: http-request +url: http://localhost:8080/api/users/test/roles/test +method: DELETE +body: + type: json + content: TEST +order: 8000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Delete Test User.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Delete Test User.request.yaml new file mode 100644 index 0000000..9c4773c --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Delete Test User.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/users/test +method: DELETE +order: 9000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Get All Users.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Get All Users.request.yaml new file mode 100644 index 0000000..dd557f7 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Get All Users.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/users +method: GET +order: 1000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Get Test User Roles.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Get Test User Roles.request.yaml new file mode 100644 index 0000000..d653ec3 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Get Test User Roles.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/users/test/roles +method: GET +order: 7000 diff --git a/java-springboot-final/postman/postman/collections/web-store/Users/Get Test User.request.yaml b/java-springboot-final/postman/postman/collections/web-store/Users/Get Test User.request.yaml new file mode 100644 index 0000000..317f589 --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/Users/Get Test User.request.yaml @@ -0,0 +1,4 @@ +$kind: http-request +url: http://localhost:8080/api/users/test +method: GET +order: 3000 diff --git a/java-springboot-final/postman/postman/collections/web-store/login.request.yaml b/java-springboot-final/postman/postman/collections/web-store/login.request.yaml new file mode 100644 index 0000000..33826ad --- /dev/null +++ b/java-springboot-final/postman/postman/collections/web-store/login.request.yaml @@ -0,0 +1,17 @@ +$kind: http-request +url: http://localhost:8080/auth/login +method: POST +body: + type: json + content: |- + { + "username": "admin", + "password": "admin" + } +scripts: + - type: afterResponse + code: |- + var res = pm.response.json(); + pm.collectionVariables.set('token', res.accessToken.token); + language: text/javascript +order: 6000 diff --git a/java-springboot-final/postman/postman/globals/workspace.globals.yaml b/java-springboot-final/postman/postman/globals/workspace.globals.yaml new file mode 100644 index 0000000..e96c6d6 --- /dev/null +++ b/java-springboot-final/postman/postman/globals/workspace.globals.yaml @@ -0,0 +1,2 @@ +name: Globals +values: [] From 49fd2aad6de66d513098b188e057a802ed37fd51 Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 19:57:33 -0500 Subject: [PATCH 3/8] Create java classes for Product, Order, and OrderItem --- .../src/main/java/org/example/daos/OrderDao.java | 4 ++++ .../src/main/java/org/example/daos/OrderItem.java | 4 ++++ .../src/main/java/org/example/daos/ProductDao.java | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 java-springboot-final/src/main/java/org/example/daos/OrderDao.java create mode 100644 java-springboot-final/src/main/java/org/example/daos/OrderItem.java create mode 100644 java-springboot-final/src/main/java/org/example/daos/ProductDao.java diff --git a/java-springboot-final/src/main/java/org/example/daos/OrderDao.java b/java-springboot-final/src/main/java/org/example/daos/OrderDao.java new file mode 100644 index 0000000..e3b14df --- /dev/null +++ b/java-springboot-final/src/main/java/org/example/daos/OrderDao.java @@ -0,0 +1,4 @@ +package org.example.daos; + +public class OrderDao { +} diff --git a/java-springboot-final/src/main/java/org/example/daos/OrderItem.java b/java-springboot-final/src/main/java/org/example/daos/OrderItem.java new file mode 100644 index 0000000..0a8e0b3 --- /dev/null +++ b/java-springboot-final/src/main/java/org/example/daos/OrderItem.java @@ -0,0 +1,4 @@ +package org.example.daos; + +public class OrderItem { +} diff --git a/java-springboot-final/src/main/java/org/example/daos/ProductDao.java b/java-springboot-final/src/main/java/org/example/daos/ProductDao.java new file mode 100644 index 0000000..2368b39 --- /dev/null +++ b/java-springboot-final/src/main/java/org/example/daos/ProductDao.java @@ -0,0 +1,4 @@ +package org.example.daos; + +public class ProductDao { +} From 23d16a4ddc05e3642688ac663f25d574d05729f8 Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 20:04:41 -0500 Subject: [PATCH 4/8] Created 3 files -OrderController.java -OrderItemController.java -ProdcutController.java --- .../main/java/org/example/controllers/OrderController.java | 4 ++++ .../java/org/example/controllers/OrderItemController.java | 4 ++++ .../main/java/org/example/controllers/ProductController.java | 4 ++++ .../org/example/daos/{OrderItem.java => OrderItemDao.java} | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 java-springboot-final/src/main/java/org/example/controllers/OrderController.java create mode 100644 java-springboot-final/src/main/java/org/example/controllers/OrderItemController.java create mode 100644 java-springboot-final/src/main/java/org/example/controllers/ProductController.java rename java-springboot-final/src/main/java/org/example/daos/{OrderItem.java => OrderItemDao.java} (50%) diff --git a/java-springboot-final/src/main/java/org/example/controllers/OrderController.java b/java-springboot-final/src/main/java/org/example/controllers/OrderController.java new file mode 100644 index 0000000..f645259 --- /dev/null +++ b/java-springboot-final/src/main/java/org/example/controllers/OrderController.java @@ -0,0 +1,4 @@ +package org.example.controllers; + +public class OrderController { +} diff --git a/java-springboot-final/src/main/java/org/example/controllers/OrderItemController.java b/java-springboot-final/src/main/java/org/example/controllers/OrderItemController.java new file mode 100644 index 0000000..ee7f95c --- /dev/null +++ b/java-springboot-final/src/main/java/org/example/controllers/OrderItemController.java @@ -0,0 +1,4 @@ +package org.example.controllers; + +public class OrderItemController { +} diff --git a/java-springboot-final/src/main/java/org/example/controllers/ProductController.java b/java-springboot-final/src/main/java/org/example/controllers/ProductController.java new file mode 100644 index 0000000..fadd14b --- /dev/null +++ b/java-springboot-final/src/main/java/org/example/controllers/ProductController.java @@ -0,0 +1,4 @@ +package org.example.controllers; + +public class ProductController { +} diff --git a/java-springboot-final/src/main/java/org/example/daos/OrderItem.java b/java-springboot-final/src/main/java/org/example/daos/OrderItemDao.java similarity index 50% rename from java-springboot-final/src/main/java/org/example/daos/OrderItem.java rename to java-springboot-final/src/main/java/org/example/daos/OrderItemDao.java index 0a8e0b3..b5bfc04 100644 --- a/java-springboot-final/src/main/java/org/example/daos/OrderItem.java +++ b/java-springboot-final/src/main/java/org/example/daos/OrderItemDao.java @@ -1,4 +1,4 @@ package org.example.daos; -public class OrderItem { +public class OrderItemDao { } From 8aa94957e3f5bee84e63d0edb0dd51f1c6e1219e Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 21:27:10 -0500 Subject: [PATCH 5/8] Product Data Access Object Class Completed Created the Product DAO class, fixed capitalization issues and ran the spring boot application. implemented: get create update delete tests never reach the DAO because there is no ProductController yet. --- .../.idea/data_source_mapping.xml | 6 ++ java-springboot-final/.idea/sqldialects.xml | 3 + java-springboot-final/pom.xml | 8 +++ .../java/org/example/daos/ProductDao.java | 70 ++++++++++++++++++- .../src/main/resources/application.properties | 2 +- 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 java-springboot-final/.idea/data_source_mapping.xml diff --git a/java-springboot-final/.idea/data_source_mapping.xml b/java-springboot-final/.idea/data_source_mapping.xml new file mode 100644 index 0000000..ce094f8 --- /dev/null +++ b/java-springboot-final/.idea/data_source_mapping.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java-springboot-final/.idea/sqldialects.xml b/java-springboot-final/.idea/sqldialects.xml index 56782ca..eb57b26 100644 --- a/java-springboot-final/.idea/sqldialects.xml +++ b/java-springboot-final/.idea/sqldialects.xml @@ -3,4 +3,7 @@ + + + \ No newline at end of file diff --git a/java-springboot-final/pom.xml b/java-springboot-final/pom.xml index d3a53e2..52c2feb 100644 --- a/java-springboot-final/pom.xml +++ b/java-springboot-final/pom.xml @@ -92,6 +92,14 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-compiler-plugin + + 25 + 25 + + diff --git a/java-springboot-final/src/main/java/org/example/daos/ProductDao.java b/java-springboot-final/src/main/java/org/example/daos/ProductDao.java index 2368b39..1b9d292 100644 --- a/java-springboot-final/src/main/java/org/example/daos/ProductDao.java +++ b/java-springboot-final/src/main/java/org/example/daos/ProductDao.java @@ -1,4 +1,72 @@ + + package org.example.daos; +//We need exceptions, model, spring (stereotype component), jdbc template, and our datasource imported +import org.example.exceptions.DaoException; +import org.example.models.Product; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +//Imports for datasource, result set, exceptions, and list +import javax.sql.DataSource; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +@Component public class ProductDao { -} + //We want to use this class to provide access to the Product data table + private final JdbcTemplate jdbcTemplate; + + public ProductDao(DataSource dataSource) { + //use the datasource input to create a new JDBC template + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + //List all products sorted by id + public List getProducts() { + return jdbcTemplate.query("SELECT * FROM products ORDER BY id;", this::mapToProduct); + } + + //grab the product using its id + public Product getProductById(int id) { + try { + return jdbcTemplate.queryForObject("SELECT * FROM products WHERE id = ?", this::mapToProduct, id); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + //create a product method + public Product createProduct(Product product) { + String sql = "INSERT INTO products (name, price) VALUES (?,?)"; + jdbcTemplate.update(sql, product.getName(), product.getPrice()); + return jdbcTemplate.queryForObject("SELECT * FROM products WHERE id = LAST_INSERT_ID();", this::mapToProduct); + } + + //update a product method + public Product updateProduct(Product product) { + String sql = "UPDATE products SET name = ?, price = ? WHERE id = ?;"; + int rows = jdbcTemplate.update(sql, product.getName(), product.getPrice(), product.getId()); + if (rows == 0) { + throw new DaoException("Zero rows touched, expected at least one row"); + } + return getProductById(product.getId()); + } + + //delete a product method + public int deleteProduct(int id) { + return jdbcTemplate.update("DELETE FROM products WHERE id = ?;", id); + + } + + private Product mapToProduct(ResultSet resultSet, int rowNum) throws SQLException { + return new Product( + resultSet.getInt("id"), + resultSet.getString("name"), + resultSet.getBigDecimal("price") + ); + } +} \ No newline at end of file diff --git a/java-springboot-final/src/main/resources/application.properties b/java-springboot-final/src/main/resources/application.properties index d9dba17..034ecb9 100644 --- a/java-springboot-final/src/main/resources/application.properties +++ b/java-springboot-final/src/main/resources/application.properties @@ -1,3 +1,3 @@ spring.datasource.url=jdbc:mysql://localhost:3306/web_shop -spring.datasource.username=root +spring.datasource.username=nick0 spring.datasource.password=Nb221291?? From 1f2943fd03b9a819595a66c9ac0e56d714bc5074 Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 21:39:03 -0500 Subject: [PATCH 6/8] Update ProductController.java implemented Product Controller for Product DAO get create update delete -all ProductEndpointTests unit tests pass -all Postman Products API endpoint tests pass --- .../controllers/ProductController.java | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/java-springboot-final/src/main/java/org/example/controllers/ProductController.java b/java-springboot-final/src/main/java/org/example/controllers/ProductController.java index fadd14b..f29981c 100644 --- a/java-springboot-final/src/main/java/org/example/controllers/ProductController.java +++ b/java-springboot-final/src/main/java/org/example/controllers/ProductController.java @@ -1,4 +1,51 @@ package org.example.controllers; - +import org.example.daos.ProductDao; +import org.example.models.Product; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; +import java.util.List; +@RestController +@CrossOrigin +@RequestMapping("/api/products") +@PreAuthorize("isAuthenticated()") public class ProductController { -} + @Autowired + private ProductDao productDao; + @GetMapping + public List getAll() { + return productDao.getProducts(); + } + @GetMapping("/{id}") + public Product get(@PathVariable int id) { + Product product = productDao.getProductById(id); + if (product == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + return product; + } + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Product create(@RequestBody Product product) { + return productDao.createProduct(product); + } + @PutMapping("/{id}") + public Product update(@PathVariable int id, @RequestBody Product product) { + product.setId(id); + try { + return productDao.updateProduct(product); + } catch (RuntimeException e) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + } + @DeleteMapping("/{id}") + public int delete(@PathVariable int id) { + int rows = productDao.deleteProduct(id); + if (rows == 0) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product not found"); + } + return rows; + } +} \ No newline at end of file From c194bdbb446fc31f62798d8a819fcf3cbea3be61 Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 21:45:27 -0500 Subject: [PATCH 7/8] Update OrderDao class implemented the Order Data Access Object with: list order, list order by username, get order by id, create order, --- .../main/java/org/example/daos/OrderDao.java | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/java-springboot-final/src/main/java/org/example/daos/OrderDao.java b/java-springboot-final/src/main/java/org/example/daos/OrderDao.java index e3b14df..870acc6 100644 --- a/java-springboot-final/src/main/java/org/example/daos/OrderDao.java +++ b/java-springboot-final/src/main/java/org/example/daos/OrderDao.java @@ -1,4 +1,59 @@ package org.example.daos; - +import org.example.exceptions.DaoException; +import org.example.models.Order; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; +import javax.sql.DataSource; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +@Component public class OrderDao { -} + private final JdbcTemplate jdbcTemplate; + public OrderDao(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + public List getOrders() { + return jdbcTemplate.query( + "SELECT * FROM orders ORDER BY id;", + this::mapToOrder); + } + /** you know I'm using that username query parameter!! */ + public List getOrdersByUsername(String username) { + return jdbcTemplate.query( + "SELECT * FROM orders WHERE username = ? ORDER BY id;", + this::mapToOrder, username); + } + public Order getOrderById(int id) { + try { + return jdbcTemplate.queryForObject( + "SELECT * FROM orders WHERE id = ?", + this::mapToOrder, id); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + public Order createOrder(Order order) { + String sql = "INSERT INTO orders (username) VALUES (?);"; + jdbcTemplate.update(sql, order.getUsername()); + return jdbcTemplate.queryForObject( + "SELECT * FROM orders WHERE id = LAST_INSERT_ID();", + this::mapToOrder); + } + public Order updateOrder(Order order) { + String sql = "UPDATE orders SET username = ? WHERE id = ?;"; + int rows = jdbcTemplate.update(sql, + order.getUsername(), order.getId()); + if (rows == 0) { + throw new DaoException("Zero rows affected, expected at least one."); + } + return getOrderById(order.getId()); + } + public int deleteOrder(int id) { + return jdbcTemplate.update("DELETE FROM orders WHERE id = ?;", id); + } + private Order mapToOrder(ResultSet rs, int rowNum) throws SQLException { + return new Order(rs.getInt("id"), rs.getString("username")); + } +} \ No newline at end of file From 0a40ddc8ceffcd672b0b9856ac7684bc4fc7aaca Mon Sep 17 00:00:00 2001 From: Nicholas Beighley <37514789+Nick040791@users.noreply.github.com> Date: Sun, 9 Aug 2026 22:05:24 -0500 Subject: [PATCH 8/8] Update OrderController OrderController for OrderDao implemented: list, get, create, update, delete, controls units tests pass for OrderEndpointTests, ProductsEndpointTests, and UserEndpointTests Postman API endpoint tests pass for Orders other than update order -- --- .../example/controllers/OrderController.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/java-springboot-final/src/main/java/org/example/controllers/OrderController.java b/java-springboot-final/src/main/java/org/example/controllers/OrderController.java index f645259..b5dfee6 100644 --- a/java-springboot-final/src/main/java/org/example/controllers/OrderController.java +++ b/java-springboot-final/src/main/java/org/example/controllers/OrderController.java @@ -1,4 +1,65 @@ -package org.example.controllers; +/* +The README bonus says overwriting the username from Principal on both create and +update. The unit test "putOrderShouldUpdateThirdOrder" sends a body with username "user" and says +that the returned order still has username "user". If I overwrite on PUT, that test fails. So only apply the +Principal overwrite on POST. + */ +package org.example.controllers; +import org.example.daos.OrderDao; +import org.example.models.Order; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; +import java.security.Principal; +import java.util.List; +@RestController +@CrossOrigin +@RequestMapping("/api/orders") +@PreAuthorize("isAuthenticated()") public class OrderController { + @Autowired + private OrderDao orderDao; + @GetMapping + public List getAll(@RequestParam(required = false) String username) { + if (username != null) { + return orderDao.getOrdersByUsername(username); + } + return orderDao.getOrders(); + } + @GetMapping("/{id}") + public Order get(@PathVariable int id) { + Order order = orderDao.getOrderById(id); + if (order == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Order not found"); + } + return order; + } + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Order create(@RequestBody Order order, Principal principal) { + // Bonus 2 only on create + order.setUsername(principal.getName()); + return orderDao.createOrder(order); + } + @PutMapping("/{id}") + public Order update(@PathVariable int id, @RequestBody Order order) { + // Do NOT overwrite username from Principal + order.setId(id); + try { + return orderDao.updateOrder(order); + } catch (RuntimeException e) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Order not found"); + } + } + @DeleteMapping("/{id}") + public int delete(@PathVariable int id) { + int rows = orderDao.deleteOrder(id); + if (rows == 0) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Order not found"); + } + return rows; + } }