Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Paysafe Interns Valentin-Martin
Shopping Basket
filip-filchev-shopping-basket-2023
Commits
b8e154d4
Commit
b8e154d4
authored
1 year ago
by
Filip Filchev
Browse files
Options
Download
Email Patches
Plain Diff
Add Post and Put Requests
Add Response Entity
parent
374064d5
master
No related merge requests found
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
src/main/java/ShoppingBasket/Database/Product/InMemoryProductDatabase.java
+2
-0
...ppingBasket/Database/Product/InMemoryProductDatabase.java
src/main/java/ShoppingBasket/Database/User/InMemoryUserDatabase.java
+2
-0
...va/ShoppingBasket/Database/User/InMemoryUserDatabase.java
src/main/java/ShoppingBasket/ShoppingBasketApplication.java
+56
-33
src/main/java/ShoppingBasket/ShoppingBasketApplication.java
src/main/java/ShoppingBasket/User/DefaultUserService.java
+4
-2
src/main/java/ShoppingBasket/User/DefaultUserService.java
src/main/java/ShoppingBasket/User/Response.java
+6
-0
src/main/java/ShoppingBasket/User/Response.java
src/main/java/ShoppingBasket/User/User.java
+8
-5
src/main/java/ShoppingBasket/User/User.java
src/main/java/ShoppingBasket/User/UserService.java
+1
-1
src/main/java/ShoppingBasket/User/UserService.java
with
79 additions
and
41 deletions
+79
-41
src/main/java/ShoppingBasket/Database/Product/InMemoryProductDatabase.java
+
2
-
0
View file @
b8e154d4
...
...
@@ -7,6 +7,7 @@ import ShoppingBasket.Database.Exceptions.ProductNotFoundException;
import
ShoppingBasket.User.Exceptions.NoOrdersByUserException
;
import
ShoppingBasket.User.Exceptions.UserNotFoundException
;
import
ShoppingBasket.User.User
;
import
org.springframework.stereotype.Repository
;
import
java.io.IOException
;
import
java.nio.file.Files
;
...
...
@@ -17,6 +18,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.stream.Stream
;
@Repository
public
class
InMemoryProductDatabase
implements
ProductDatabase
{
private
static
final
int
AMOUNT
=
2
;
private
static
final
String
DUMMY_PASSWORD
=
"alabala"
;
...
...
This diff is collapsed.
Click to expand it.
src/main/java/ShoppingBasket/Database/User/InMemoryUserDatabase.java
+
2
-
0
View file @
b8e154d4
...
...
@@ -3,6 +3,7 @@ package ShoppingBasket.Database.User;
import
ShoppingBasket.User.Exceptions.UserAlreadyRegisteredException
;
import
ShoppingBasket.User.Exceptions.UserNotFoundException
;
import
ShoppingBasket.User.User
;
import
org.springframework.stereotype.Repository
;
import
java.io.IOException
;
import
java.nio.file.Files
;
...
...
@@ -14,6 +15,7 @@ import java.util.stream.Stream;
/*
Mimics a DataBase
*/
@Repository
public
class
InMemoryUserDatabase
implements
UserDatabase
{
private
static
final
String
USERS_FILE_NAME
=
"users.csv"
;
private
final
Set
<
User
>
users
;
...
...
This diff is collapsed.
Click to expand it.
src/main/java/ShoppingBasket/ShoppingBasketApplication.java
+
56
-
33
View file @
b8e154d4
...
...
@@ -2,36 +2,46 @@ package ShoppingBasket;
import
ShoppingBasket.Basket.Basket
;
import
ShoppingBasket.Basket.Product
;
import
ShoppingBasket.Database.Product.InMemoryProductDatabase
;
import
ShoppingBasket.Database.Product.ProductDatabase
;
import
ShoppingBasket.Database.User.InMemoryUserDatabase
;
import
ShoppingBasket.Database.User.UserDatabase
;
import
ShoppingBasket.User.DefaultUserService
;
import
ShoppingBasket.User.Exceptions.InvalidRequestException
;
import
ShoppingBasket.User.Exceptions.UserNotFoundException
;
import
ShoppingBasket.User.Exceptions.UserNotLoggedInException
;
import
ShoppingBasket.User.Exceptions.UserNotRegisteredException
;
import
ShoppingBasket.User.Response
;
import
ShoppingBasket.User.User
;
import
ShoppingBasket.User.UserService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PutMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
import
java.util.Map
;
@SpringBootApplication
public
class
ShoppingBasketApplication
{
private
static
final
String
DUMMY_PASSWORD
=
"alabala"
;
private
static
ConfigurableApplicationContext
cac
;
private
final
UserDatabase
userDatabase
=
new
InMemoryUserDatabase
();
private
final
ProductDatabase
productDatabase
=
new
InMemoryProductDatabase
();
private
final
UserService
userService
=
new
DefaultUserService
();
private
final
UserDatabase
userDatabase
;
private
final
ProductDatabase
productDatabase
;
private
final
UserService
userService
;
@Autowired
public
ShoppingBasketApplication
(
UserDatabase
userDatabase
,
ProductDatabase
productDatabase
,
UserService
userService
)
{
this
.
userDatabase
=
userDatabase
;
this
.
productDatabase
=
productDatabase
;
this
.
userService
=
userService
;
}
public
static
void
main
(
String
[]
args
)
{
cac
=
SpringApplication
.
run
(
ShoppingBasketApplication
.
class
,
args
);
...
...
@@ -69,65 +79,71 @@ public class ShoppingBasketApplication {
@RestController
public
class
UserController
{
@GetMapping
(
"/register"
)
public
String
register
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"password"
,
defaultValue
=
"unknown"
)
String
password
)
{
@PostMapping
(
"/register"
)
public
ResponseEntity
<
Response
<
User
>>
register
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"password"
,
defaultValue
=
"unknown"
)
String
password
)
{
System
.
out
.
println
(
username
+
" requested register"
);
User
user
;
try
{
validateRequestUsername
(
username
);
validateRequestPassword
(
password
);
userDatabase
.
registerUser
(
username
,
password
);
User
user
=
new
User
(
username
,
password
);
user
=
new
User
(
username
,
password
);
productDatabase
.
addNewUser
(
user
);
userService
.
logIn
(
user
,
password
);
userService
.
logIn
(
user
);
}
catch
(
Exception
e
)
{
return
e
.
getMessage
();
return
ResponseEntity
.
badRequest
().
body
(
new
Response
<>(
e
.
getMessage
()
,
null
))
;
}
return
"
Re
gistered and Logged in S
uccessfully"
;
return
Re
sponseEntity
.
ok
().
body
(
new
Response
<>(
"User registered s
uccessfully"
,
user
))
;
}
@
Ge
tMapping
(
"/login"
)
public
String
login
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"password"
,
defaultValue
=
"unknown"
)
String
password
)
{
System
.
out
.
println
(
username
+
" requested login"
);
@
Pu
tMapping
(
"/login"
)
public
ResponseEntity
<
Response
<
User
>>
login
(
@RequestBody
User
user
)
{
System
.
out
.
println
(
user
.
getUser
name
()
+
" requested login"
);
try
{
validateRequestUsername
(
username
);
validateRequestPassword
(
password
);
User
user
=
userDatabase
.
getUser
(
username
);
if
(!
userDatabase
.
isRegistered
(
user
))
{
return
ResponseEntity
.
badRequest
().
body
(
new
Response
<>(
"User not registered"
,
user
));
}
productDatabase
.
addNewUser
(
user
);
validateRegistered
(
user
);
userService
.
logIn
(
user
,
password
);
userService
.
logIn
(
user
);
}
catch
(
Exception
e
)
{
return
e
.
getMessage
();
return
ResponseEntity
.
badRequest
().
body
(
new
Response
<>(
e
.
getMessage
(),
user
));
}
return
"Logged In Successfully"
;
return
ResponseEntity
.
ok
().
body
(
new
Response
<>(
"User logged in successfully"
,
user
));
}
@GetMapping
(
"/logout"
)
public
String
logout
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
)
{
public
ResponseEntity
<
Response
<
User
>>
logout
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
)
{
System
.
out
.
println
(
username
+
" requested logout"
);
User
user
;
try
{
validateRequestUsername
(
username
);
User
user
=
new
User
(
username
,
DUMMY_PASSWORD
);
user
=
new
User
(
username
,
DUMMY_PASSWORD
);
validateLoggedIn
(
user
);
userService
.
logOut
(
user
);
}
catch
(
Exception
e
)
{
return
e
.
getMessage
();
return
ResponseEntity
.
badRequest
().
body
(
new
Response
<>(
e
.
getMessage
(),
null
));
}
return
"L
ogged
O
ut
S
uccessfully"
;
return
ResponseEntity
.
ok
().
body
(
new
Response
<>(
"User l
ogged
o
ut
s
uccessfully"
,
user
))
;
}
}
...
...
@@ -135,7 +151,8 @@ public class ShoppingBasketApplication {
public
class
RequestsController
{
@GetMapping
(
"/deposit"
)
public
String
deposit
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"amount"
,
defaultValue
=
"unknown"
)
String
amount
)
{
public
String
deposit
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"amount"
,
defaultValue
=
"unknown"
)
String
amount
)
{
System
.
out
.
println
(
username
+
" requested deposit: "
+
amount
);
User
user
;
...
...
@@ -220,7 +237,8 @@ public class ShoppingBasketApplication {
}
@GetMapping
(
"/search"
)
public
String
search
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"keyword"
,
defaultValue
=
"unknown"
)
String
keyword
)
{
public
String
search
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"keyword"
,
defaultValue
=
"unknown"
)
String
keyword
)
{
System
.
out
.
println
(
username
+
" requested search"
);
User
user
;
...
...
@@ -239,7 +257,9 @@ public class ShoppingBasketApplication {
}
@GetMapping
(
"/add"
)
public
String
add
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"product"
,
defaultValue
=
"unknown"
)
String
product
,
@RequestParam
(
value
=
"amount"
,
defaultValue
=
"unknown"
)
String
amount
)
{
public
String
add
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"product"
,
defaultValue
=
"unknown"
)
String
product
,
@RequestParam
(
value
=
"amount"
,
defaultValue
=
"unknown"
)
String
amount
)
{
System
.
out
.
println
(
username
+
" requested add"
);
User
user
;
...
...
@@ -271,7 +291,9 @@ public class ShoppingBasketApplication {
}
@GetMapping
(
"/remove"
)
public
String
remove
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"product"
,
defaultValue
=
"unknown"
)
String
product
,
@RequestParam
(
value
=
"amount"
,
defaultValue
=
"unknown"
)
String
amount
)
{
public
String
remove
(
@RequestParam
(
value
=
"username"
,
defaultValue
=
"unknown"
)
String
username
,
@RequestParam
(
value
=
"product"
,
defaultValue
=
"unknown"
)
String
product
,
@RequestParam
(
value
=
"amount"
,
defaultValue
=
"unknown"
)
String
amount
)
{
System
.
out
.
println
(
username
+
" requested remove"
);
User
user
;
...
...
@@ -410,7 +432,8 @@ public class ShoppingBasketApplication {
return
e
.
getMessage
();
}
StringBuilder
builder
=
new
StringBuilder
(
"product,price,amount...:total price"
).
append
(
System
.
lineSeparator
());
StringBuilder
builder
=
new
StringBuilder
(
"product,price,amount...:total price"
).
append
(
System
.
lineSeparator
());
for
(
String
order
:
orders
)
{
builder
.
append
(
order
).
append
(
System
.
lineSeparator
());
...
...
This diff is collapsed.
Click to expand it.
src/main/java/ShoppingBasket/User/DefaultUserService.java
+
4
-
2
View file @
b8e154d4
...
...
@@ -3,10 +3,12 @@ package ShoppingBasket.User;
import
ShoppingBasket.User.Exceptions.InvalidPasswordException
;
import
ShoppingBasket.User.Exceptions.UserAlreadyLoggedInException
;
import
ShoppingBasket.User.Exceptions.UserNotLoggedInException
;
import
org.springframework.stereotype.Service
;
import
java.util.HashSet
;
import
java.util.Set
;
@Service
public
class
DefaultUserService
implements
UserService
{
private
final
Set
<
User
>
loggedInUsers
;
...
...
@@ -15,12 +17,12 @@ public class DefaultUserService implements UserService {
}
@Override
public
void
logIn
(
User
user
,
String
password
)
throws
UserAlreadyLoggedInException
,
InvalidPasswordException
{
public
void
logIn
(
User
user
)
throws
UserAlreadyLoggedInException
,
InvalidPasswordException
{
if
(
loggedInUsers
.
contains
(
user
))
{
throw
new
UserAlreadyLoggedInException
(
"User with username: "
+
user
.
getUsername
()
+
" is already logged in"
);
}
if
(!
user
.
comparePassword
(
password
))
{
if
(!
user
.
comparePassword
(
user
))
{
throw
new
InvalidPasswordException
(
"Incorrect password for User with Username: "
+
user
.
getUsername
());
}
...
...
This diff is collapsed.
Click to expand it.
src/main/java/ShoppingBasket/User/Response.java
0 → 100644
+
6
-
0
View file @
b8e154d4
package
ShoppingBasket.User
;
import
com.fasterxml.jackson.annotation.JsonProperty
;
public
record
Response
<
T
>(
@JsonProperty
(
"ResponseMessage"
)
String
message
,
T
object
)
{
}
This diff is collapsed.
Click to expand it.
src/main/java/ShoppingBasket/User/User.java
+
8
-
5
View file @
b8e154d4
package
ShoppingBasket.User
;
import
ShoppingBasket.User.Exceptions.NotEnoughBalanceException
;
import
org.springframework.stereotype.Component
;
import
java.util.Objects
;
...
...
@@ -41,7 +42,7 @@ public class User {
}
public
double
withdraw
(
double
sum
)
throws
NotEnoughBalanceException
{
if
(
balance
<
sum
)
{
if
(
balance
<
sum
)
{
throw
new
NotEnoughBalanceException
(
"Not enough balance for the transaction"
);
}
...
...
@@ -58,14 +59,16 @@ public class User {
return
balance
;
}
public
boolean
comparePassword
(
String
password
)
{
return
this
.
password
.
equals
(
password
);
public
boolean
comparePassword
(
User
user
)
{
return
this
.
password
.
equals
(
user
.
password
);
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
User
user
=
(
User
)
o
;
return
Objects
.
equals
(
username
,
user
.
username
);
}
...
...
This diff is collapsed.
Click to expand it.
src/main/java/ShoppingBasket/User/UserService.java
+
1
-
1
View file @
b8e154d4
...
...
@@ -6,7 +6,7 @@ import ShoppingBasket.User.Exceptions.UserNotLoggedInException;
public
interface
UserService
{
void
logIn
(
User
user
,
String
password
)
throws
UserAlreadyLoggedInException
,
InvalidPasswordException
;
void
logIn
(
User
user
)
throws
UserAlreadyLoggedInException
,
InvalidPasswordException
;
void
logOut
(
User
user
)
throws
UserNotLoggedInException
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help