Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
E
Ecsite
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shoei.kanno
Ecsite
Commits
3d5eac0e
Commit
3d5eac0e
authored
Oct 21, 2020
by
keita.onoguchi
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '購入機能' into 'master'
購入数分、靴のストックを減らす処理を追加 See merge request
!47
parents
d4f04b14
3b1db15a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
105 additions
and
7 deletions
+105
-7
ControllerExceptionHandler.java
...main/java/com/example/api/ControllerExceptionHandler.java
+36
-0
EcsiteRestController.java
src/main/java/com/example/api/EcsiteRestController.java
+27
-2
BadRequestException.java
src/main/java/com/example/domain/BadRequestException.java
+13
-0
ErrorResponse.java
src/main/java/com/example/domain/ErrorResponse.java
+19
-0
SalesLogService.java
src/main/java/com/example/service/SalesLogService.java
+10
-5
No files found.
src/main/java/com/example/api/ControllerExceptionHandler.java
0 → 100644
View file @
3d5eac0e
package
com
.
example
.
api
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.context.request.WebRequest
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
import
com.example.domain.ErrorResponse
;
@RestControllerAdvice
public
class
ControllerExceptionHandler
extends
ResponseEntityExceptionHandler
{
@Override
protected
ResponseEntity
<
Object
>
handleExceptionInternal
(
Exception
ex
,
Object
body
,
HttpHeaders
headers
,
HttpStatus
status
,
WebRequest
request
)
{
if
(!(
body
instanceof
ErrorResponse
))
{
if
(
status
.
getReasonPhrase
().
equalsIgnoreCase
(
"Method Not Allowed"
)){
body
=
new
Error
(
"URLの誤り"
);
status
=
HttpStatus
.
NOT_FOUND
;
}
}
return
new
ResponseEntity
<>(
body
,
headers
,
status
);
}
@ExceptionHandler
({
Exception
.
class
})
public
ResponseEntity
<
Object
>
handle400
(
Exception
ex
,
WebRequest
request
)
{
HttpHeaders
headers
=
new
HttpHeaders
();
ErrorResponse
body
=
new
ErrorResponse
(
"これ以上購入できません"
);
HttpStatus
status
=
HttpStatus
.
BAD_REQUEST
;
return
this
.
handleExceptionInternal
(
ex
,
body
,
headers
,
status
,
request
);
}
}
\ No newline at end of file
src/main/java/com/example/api/EcsiteRestController.java
View file @
3d5eac0e
...
...
@@ -18,13 +18,16 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.SessionAttributes
;
import
com.example.domain.BadRequestException
;
import
com.example.domain.ItemData
;
import
com.example.domain.Items
;
import
com.example.domain.Shoes
;
import
com.example.domain.User
;
import
com.example.service.ItemsService
;
import
com.example.service.LoginUser
;
import
com.example.service.LoginUserDetailsService
;
import
com.example.service.SalesLogService
;
import
com.example.service.ShoesService
;
@RestController
@RequestMapping
(
"limited"
)
...
...
@@ -38,8 +41,11 @@ public class EcsiteRestController {
HttpSession
session
;
@Autowired
SalesLogService
salesLogService
;
@Autowired
ShoesService
shoesService
;
int
totalPrice
;
//アカウント新規登録
@PostMapping
(
"signUp"
)
public
User
createUser
(
@RequestBody
User
user
)
{
user
.
setRoles
(
"USER"
);
...
...
@@ -52,28 +58,45 @@ public class EcsiteRestController {
return
user
;
}
//カート追加処理
@PostMapping
(
"inputCart"
)
public
void
inputCart
(
@RequestBody
ItemData
data
,
Model
model
)
{
public
void
inputCart
(
@RequestBody
ItemData
data
,
Model
model
,
Shoes
shoes
)
{
//靴の在庫数を購入予定数が超えていないか確認
shoes
=
shoesService
.
findOne
(
data
.
getShoesId
());
if
(
shoes
.
getStock
()
<
data
.
getQuantity
()){
throw
new
BadRequestException
();
}
//セッションがnullの場合
if
(
session
.
getAttribute
(
"cart"
)
==
null
){
//HashMapを生成
LinkedHashMap
<
String
,
Items
>
cart
=
new
LinkedHashMap
<
String
,
Items
>();
String
id
=
data
.
getShoesId
().
toString
();
//HashMapに靴のIdをKeyとして、Valueに靴の情報を入れる。
cart
.
put
(
id
,
itemsService
.
findOne
(
data
.
getShoesId
(),
data
.
getQuantity
()));
System
.
out
.
println
(
cart
);
//HashMapをセッションに追加
session
.
setAttribute
(
"cart"
,
cart
);
//カートのアイテム数をセッションに追加
session
.
setAttribute
(
"cartValue"
,
data
.
getQuantity
());
//セッションがnull出ない場合
}
else
{
//HashMapを生成
LinkedHashMap
<
String
,
Items
>
cart
=
new
LinkedHashMap
<
String
,
Items
>();
String
id
=
data
.
getShoesId
().
toString
();
//生成したHashMapにセッションにもともとある情報を照会させる。
cart
=
(
LinkedHashMap
<
String
,
Items
>)
session
.
getAttribute
(
"cart"
);
//HashMapに靴のIdをKeyとして、Valueに靴の情報を入れる。
cart
.
put
(
id
,
itemsService
.
findOne
(
data
.
getShoesId
(),
data
.
getQuantity
()));
//HashMapをセッションに追加
session
.
setAttribute
(
"cart"
,
cart
);
int
nowQuantity
=
(
int
)
session
.
getAttribute
(
"cartValue"
);
//カートのアイテム数をセッションに追加
session
.
setAttribute
(
"cartValue"
,
nowQuantity
+
data
.
getQuantity
());
}
}
@GetMapping
(
"buy"
)
public
void
buy
(
@AuthenticationPrincipal
LoginUser
userDetails
,
LinkedHashMap
<
String
,
Items
>
items
,
ArrayList
<
Items
>
cart
){
public
String
buy
(
@AuthenticationPrincipal
LoginUser
userDetails
,
LinkedHashMap
<
String
,
Items
>
items
,
ArrayList
<
Items
>
cart
){
items
=
(
LinkedHashMap
<
String
,
Items
>)
session
.
getAttribute
(
"cart"
);
items
.
forEach
((
key
,
value
)
->
{
cart
.
add
(
value
);
...
...
@@ -82,5 +105,6 @@ public class EcsiteRestController {
//Integer userId = user.getId();
salesLogService
.
update
(
1
,
cart
);
session
.
removeAttribute
(
"cart"
);
return
"index"
;
}
}
\ No newline at end of file
src/main/java/com/example/domain/BadRequestException.java
0 → 100644
View file @
3d5eac0e
package
com
.
example
.
domain
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
class
BadRequestException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
1L
;
public
BadRequestException
()
{
super
();
}
}
\ No newline at end of file
src/main/java/com/example/domain/ErrorResponse.java
0 → 100644
View file @
3d5eac0e
package
com
.
example
.
domain
;
import
lombok.Value
;
@Value
public
class
ErrorResponse
{
//@JsonProperty("Error")
//private Error error;
private
final
String
message
;
/*public ErrorResponse(String message) {
this.error = new Error(message);
}
@Value
private class Error {
}*/
}
\ No newline at end of file
src/main/java/com/example/service/SalesLogService.java
View file @
3d5eac0e
...
...
@@ -5,17 +5,19 @@ import java.util.ArrayList;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.core.annotation.AuthenticationPrincipal
;
import
org.springframework.stereotype.Service
;
import
com.example.domain.Items
;
import
com.example.domain.SalesLog
;
import
com.example.domain.Shoes
;
import
com.example.repository.SalesLogRepository
;
@Service
public
class
SalesLogService
{
@Autowired
SalesLogRepository
salesLogRepository
;
@Autowired
ShoesService
shoesService
;
public
List
<
SalesLog
>
findAll
(){
return
salesLogRepository
.
findAll
();
...
...
@@ -30,21 +32,24 @@ public class SalesLogService {
}
public
void
update
(
Integer
userId
,
ArrayList
<
Items
>
cart
){
int
count
=
0
;
java
.
util
.
Date
d1
=
new
java
.
util
.
Date
();
//d1をSQL.DATE型に変換してtodayに代入
java
.
sql
.
Date
today
=
new
java
.
sql
.
Date
(
d1
.
getTime
());
System
.
out
.
println
(
cart
.
size
());
//カートに入っている靴の種類文処理
for
(
int
i
=
0
;
i
<
cart
.
size
();
i
++){
//在庫数を購入数分減らす処理
Shoes
shoes
=
new
Shoes
();
shoes
=
shoesService
.
findOne
((
cart
.
get
(
i
)).
getShoesId
());
shoes
.
setStock
(
shoes
.
getStock
()
-
(
cart
.
get
(
i
)).
getQuantity
());
shoesService
.
update
(
shoes
);
//購入履歴に情報を追加する処理
SalesLog
salesLog
=
new
SalesLog
();
salesLog
.
setUserId
(
userId
);
salesLog
.
setShoesId
((
cart
.
get
(
i
)).
getShoesId
());
salesLog
.
setPrice
((
cart
.
get
(
i
)).
getPrice
());
salesLog
.
setCreated
(
today
);
salesLogRepository
.
save
(
salesLog
);
count
++;
}
System
.
out
.
println
(
count
);
}
public
List
<
SalesLog
>
history
(
Integer
id
)
{
...
...
Write
Preview
Markdown
is supported
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