Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
idatt2106-v23-03
backend
Commits
047697bb
Commit
047697bb
authored
1 year ago
by
Birk Øvstetun Narvhus
Browse files
Options
Downloads
Patches
Plain Diff
added authentication to the rest of fridge endpoints
parent
5135cfa9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java
+60
-5
60 additions, 5 deletions
...2016/v233/SmartMat/controller/group/FridgeController.java
with
60 additions
and
5 deletions
src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java
+
60
−
5
View file @
047697bb
...
...
@@ -79,7 +79,20 @@ public class FridgeController {
* @return success if the product was added, bad request if the product was already in the fridge, or not found if the group or product doesn't exist
*/
@PostMapping
(
"/group/product"
)
public
ResponseEntity
<
Product
>
addProductToFridge
(
@RequestBody
FridgeProductRequest
request
)
{
public
ResponseEntity
<
Product
>
addProductToFridge
(
@RequestBody
FridgeProductRequest
request
,
Authentication
authentication
)
{
Optional
<
Fridge
>
fridge
=
fridgeService
.
getFridgeByGroupId
(
request
.
groupId
());
if
(
fridge
.
isEmpty
())
{
return
ResponseEntity
.
notFound
().
build
();
}
if
(!
fridgeService
.
isUserInFridge
(
authentication
.
getName
(),
fridge
.
get
().
getFridgeId
())
&&
!
authentication
.
getAuthorities
().
contains
(
new
SimpleGrantedAuthority
(
Authority
.
ADMIN
.
name
())))
{
return
ResponseEntity
.
status
(
HttpStatus
.
FORBIDDEN
).
build
();
}
try
{
return
fridgeService
.
addProductToFridge
(
request
).
map
(
ResponseEntity:
:
ok
).
orElseGet
(()
->
ResponseEntity
.
notFound
().
build
());
}
catch
(
IllegalArgumentException
e
)
{
...
...
@@ -87,11 +100,37 @@ public class FridgeController {
}
}
/**
* Updates a product in a fridge
* @param request the request containing the group id and product id
* @return success if the product was added, bad request if the product was already in the fridge,
* or not found if the group or product doesn't exist
*/
@PutMapping
(
"/group/product"
)
public
ResponseEntity
<
FridgeProductAsso
>
updateProductInFridge
(
@RequestBody
FridgeProductRequest
request
)
{
public
ResponseEntity
<
FridgeProductAsso
>
updateProductInFridge
(
@RequestBody
FridgeProductRequest
request
,
Authentication
authentication
)
{
Optional
<
Fridge
>
fridge
=
fridgeService
.
getFridgeByGroupId
(
request
.
groupId
());
if
(
fridge
.
isEmpty
())
{
return
ResponseEntity
.
notFound
().
build
();
}
if
(!
fridgeService
.
isUserInFridge
(
authentication
.
getName
(),
fridge
.
get
().
getFridgeId
())
&&
!
authentication
.
getAuthorities
().
contains
(
new
SimpleGrantedAuthority
(
Authority
.
ADMIN
.
name
())))
{
return
ResponseEntity
.
status
(
HttpStatus
.
FORBIDDEN
).
build
();
}
return
fridgeService
.
updateProductInFridge
(
request
).
map
(
ResponseEntity:
:
ok
).
orElseGet
(()->
ResponseEntity
.
notFound
().
build
());
}
/**
* Deletes an amount of a product from a fridge
* @param fridgeProductId the id of the fridge product to delete
* @param amountStr the amount to delete
* @param authentication the authentication of the user
* @return 200 if the amount was deleted, 404 if the fridge product doesn't exist, 403 if the user is not in the group
*/
@DeleteMapping
(
"/group/delete/product/{fridgeProductId}/{amount}"
)
public
ResponseEntity
<?>
deleteAmountFridgeProduct
(
@PathVariable
(
"fridgeProductId"
)
long
fridgeProductId
,
@PathVariable
(
"amount"
)
String
amountStr
,
Authentication
authentication
)
{
...
...
@@ -122,9 +161,17 @@ public class FridgeController {
* Deletes a product from the fridge
* @param fridgeProductId the id of the fridge product association
* @return success if the product was deleted, bad request if the product wasn't found
* , or forbidden if the user is not in the group
*/
@DeleteMapping
(
"/delete/product/{fridgeProductId}"
)
public
ResponseEntity
<
String
>
removeProductFromFridge
(
@PathVariable
(
"fridgeProductId"
)
long
fridgeProductId
)
{
public
ResponseEntity
<
String
>
removeProductFromFridge
(
@PathVariable
(
"fridgeProductId"
)
long
fridgeProductId
,
Authentication
authentication
)
{
if
(!
fridgeService
.
isUserInGroupWithFridgeProduct
(
authentication
.
getName
(),
fridgeProductId
)
&&
!
authentication
.
getAuthorities
().
contains
(
new
SimpleGrantedAuthority
(
Authority
.
ADMIN
.
name
()))){
return
ResponseEntity
.
status
(
403
).
body
(
"You are not a member of this group"
);
}
try
{
boolean
success
=
fridgeService
.
removeProductFromFridge
(
fridgeProductId
);
if
(
success
){
...
...
@@ -140,10 +187,18 @@ public class FridgeController {
* Deletes a product from the fridge and creates a waste object from it.
*
* @param fridgeProductId The id of the fridge product association to be deleted
* @return A ResponseEntity with status code 200 if successful, or status code 404 if the specified fridge product association was not found.
* @return A ResponseEntity with status code 200 if successful,
* or status code 404 if the specified fridge product association was not found.
* or status code 403 if the user is not in the group
*/
@DeleteMapping
(
"/waste/product/{fridgeProductId}"
)
public
ResponseEntity
<?>
wasteProductFromFridge
(
@PathVariable
(
"fridgeProductId"
)
long
fridgeProductId
){
public
ResponseEntity
<?>
wasteProductFromFridge
(
@PathVariable
(
"fridgeProductId"
)
long
fridgeProductId
,
Authentication
authentication
){
if
(!
fridgeService
.
isUserInGroupWithFridgeProduct
(
authentication
.
getName
(),
fridgeProductId
)
&&
!
authentication
.
getAuthorities
().
contains
(
new
SimpleGrantedAuthority
(
Authority
.
ADMIN
.
name
()))){
return
ResponseEntity
.
status
(
403
).
body
(
"You are not a member of this group"
);
}
return
fridgeService
.
wasteProductFromFridge
(
fridgeProductId
).
map
(
ResponseEntity:
:
ok
).
orElseGet
(()
->
ResponseEntity
.
notFound
().
build
());
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment