Skip to content

Commit 82a1e62

Browse files
committed
FullStack : Backend Shopping Mall
1 parent 42e6b50 commit 82a1e62

38 files changed

+1051
-19
lines changed

Part-9.SpringBoot-React-Projects/Project-2.SpringBoot-React-ShoppingMall/fullstack/backend/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@
7070
<scope>test</scope>
7171
</dependency>
7272

73+
<dependency>
74+
<groupId>io.springfox</groupId>
75+
<artifactId>springfox-swagger2</artifactId>
76+
<version>2.9.2</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>io.springfox</groupId>
81+
<artifactId>springfox-swagger-ui</artifactId>
82+
<version>2.9.2</version>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>org.springframework.kafka</groupId>
87+
<artifactId>spring-kafka</artifactId>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>org.springframework.kafka</groupId>
92+
<artifactId>spring-kafka-test</artifactId>
93+
<scope>test</scope>
94+
</dependency>
7395
<!---->
7496

7597

Part-9.SpringBoot-React-Projects/Project-2.SpringBoot-React-ShoppingMall/fullstack/backend/src/main/java/com/urunov/ShoppingApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
56

67
@SpringBootApplication
8+
@EnableSwagger2
79
public class ShoppingApplication {
810

911
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.urunov.configure;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
7+
import springfox.documentation.builders.ApiInfoBuilder;
8+
import springfox.documentation.builders.RequestHandlerSelectors;
9+
import springfox.documentation.service.ApiInfo;
10+
import springfox.documentation.service.Contact;
11+
import springfox.documentation.spi.DocumentationType;
12+
import springfox.documentation.spring.web.plugins.Docket;
13+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
14+
15+
import static springfox.documentation.builders.PathSelectors.regex;
16+
17+
/**
18+
* Created by:
19+
* User: hamdamboy
20+
* Project: IntelliJ IDEA
21+
* Github: @urunov
22+
*/
23+
24+
@Configuration
25+
@EnableSwagger2
26+
public class SwaggerConfig extends WebMvcConfigurationSupport {
27+
28+
@Bean
29+
public Docket postsApi() {
30+
31+
return new Docket(DocumentationType.SWAGGER_2)
32+
.select()
33+
.apis(RequestHandlerSelectors.basePackage("com.urunov.controller"))
34+
.paths(regex("/api.*"))
35+
.build()
36+
.apiInfo(metaData());
37+
}
38+
39+
private ApiInfo metaData() {
40+
return new ApiInfoBuilder()
41+
.title("Spring Boot REST API with Swagger")
42+
.description("\"Spring Boot REST API for Employee")
43+
.version("1.0.0")
44+
.license("Apache License Version 2.0")
45+
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
46+
.contact(new Contact("Urunov Hamdamboy", "https://github.com/urunov/", "myindexu@gamil.com"))
47+
.build();
48+
}
49+
50+
@Override
51+
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
52+
registry.addResourceHandler("swagger-ui.html")
53+
.addResourceLocations("classpath:/META-INF/resources/");
54+
registry.addResourceHandler("/webjars/**")
55+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.urunov.controller;
2+
3+
import com.urunov.model.Good;
4+
import com.urunov.payload.PagedResponse;
5+
import com.urunov.payload.good.GoodResponseForRetailer;
6+
import com.urunov.repository.GoodsRepository;
7+
import com.urunov.repository.RetailerRepository;
8+
import com.urunov.repository.UserRepository;
9+
import com.urunov.security.CurrentUser;
10+
import com.urunov.security.UserPrincipal;
11+
import com.urunov.service.CataloguesService;
12+
import com.urunov.utils.AppConstants;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestParam;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
/**
20+
* Created by:
21+
* User: hamdamboy
22+
* Project: IntelliJ IDEA
23+
* Github: @urunov
24+
*/
25+
26+
@RestController
27+
@RequestMapping("/api")
28+
public class CatalogueController {
29+
30+
@Autowired
31+
private GoodsRepository goodsRepository;
32+
33+
@Autowired
34+
private UserRepository userRepository;
35+
36+
@Autowired
37+
private RetailerRepository retailerRepository;
38+
39+
@Autowired
40+
private CataloguesService cataloguesService;
41+
42+
public PagedResponse<GoodResponseForRetailer> getCatalogueOfGoods(
43+
@RequestParam(value = "page", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) int page,
44+
@RequestParam(value = "size", defaultValue = AppConstants.DEFAULT_PAGE_SIZE) int size,
45+
@RequestParam(defaultValue = "updatedAt") String sortBy,
46+
@RequestParam(defaultValue = "descend") String sortOrder)
47+
{
48+
return cataloguesService.getCatalogueOfGoods(page, size, sortBy, sortOrder);
49+
}
50+
51+
@GetMapping("/getGoodById")
52+
public Good getGoodById(@RequestParam(value = "goodId"), String goodId){
53+
return goodsRepository.findGoodById(Long.valueOf(goodId));
54+
}
55+
56+
public PagedResponse<GoodResponseForRetailer> getGoodsByRetailers
57+
(
58+
@CurrentUser UserPrincipal userPrincipal, @RequestParam(value = "retailersId") Long id,
59+
@RequestParam(value = "page", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) int page,
60+
@RequestParam(value = "size", defaultValue = AppConstants.DEFAULT_PAGE_SIZE) int size,
61+
@RequestParam(defaultValue = "updatedAt") String sortBy,
62+
@RequestParam(defaultValue = "descend") String sortOrder,
63+
@RequestParam String filteredValue)
64+
{
65+
return cataloguesService.getGoodsByRetailers(userPrincipal, id, page, size, sortBy, sortOrder, filteredValue);
66+
}
67+
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//package com.urunov.controller;
2+
//
3+
//import com.urunov.payload.order.OrderResponse;
4+
//import com.urunov.repository.GoodsRepository;
5+
//import com.urunov.repository.OrderDetailsRepository;
6+
//import com.urunov.repository.OrderRepository;
7+
//import com.urunov.repository.UserRepository;
8+
//import com.urunov.security.CurrentUser;
9+
//import com.urunov.security.UserPrincipal;
10+
//import com.urunov.service.taxiMaster.TaxiOrderProcess;
11+
//import org.springframework.beans.factory.annotation.Autowired;
12+
//import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
13+
//import org.springframework.web.bind.annotation.GetMapping;
14+
//import org.springframework.web.bind.annotation.RequestMapping;
15+
//import org.springframework.web.bind.annotation.RequestParam;
16+
//import org.springframework.web.bind.annotation.RestController;
17+
//import com.urunov.utils.AppConstants;
18+
///**
19+
// * Created by:
20+
// * User: hamdamboy
21+
// * Project: IntelliJ IDEA
22+
// * Github: @urunov
23+
// */
24+
//
25+
//@RestController
26+
//@RequestMapping("/api")
27+
//public class OrderController {
28+
//
29+
// @Autowired
30+
// private OrderRepository orderRepository;
31+
//
32+
// @Autowired
33+
// private UserRepository userRepository;
34+
//
35+
// @Autowired
36+
// private OrderDetailsRepository orderDetailsRepository;
37+
//
38+
// @Autowired
39+
// private TaxiOrderProcess taxiOrderProcess;
40+
//
41+
// @Autowired
42+
// private KafkaProperties.Producer producer;
43+
//
44+
// @Autowired
45+
// private OrderService orderService;
46+
//
47+
// @Autowired
48+
// private GoodsRepository goodsRepository;
49+
//
50+
// @Autowired
51+
// private PaymentService paymentService;
52+
//
53+
// @GetMapping("/order/getAllUserOrders")
54+
// public PagedResponse<OrderResponse> getAllUserOrders(
55+
// @RequestParam(value = "page", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) int page,
56+
// @RequestParam(value = "size", defaultValue = AppConstants.DEFAULT_PAGE_SIZE) int size,
57+
// @RequestParam(defaultValue = "createdAt") String sortBy,
58+
// @RequestParam(defaultValue ="descend") String sortOrder,
59+
// @RequestParam(value = "isActive", defaultValue = "true") Boolean isActive,
60+
// @CurrentUser UserPrincipal userPrincipal)
61+
// {
62+
// return orderService.getAllUserOrders(page, size, sortOrder, isActive, userPrincipal, "status", sortBy);
63+
// }
64+
//
65+
//
66+
//}

Part-9.SpringBoot-React-Projects/Project-2.SpringBoot-React-ShoppingMall/fullstack/backend/src/main/java/com/urunov/controller/UserController.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.urunov.controller;
22

3+
import com.urunov.exception.ResourceNotFoundException;
4+
import com.urunov.model.User;
35
import com.urunov.payload.UserIdentityAvailability;
6+
import com.urunov.payload.UserProfile;
47
import com.urunov.payload.UserSummary;
58
import com.urunov.repository.UserRepository;
69
import com.urunov.security.CurrentUser;
@@ -10,11 +13,12 @@
1013
import org.slf4j.Logger;
1114
import org.slf4j.LoggerFactory;
1215
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.http.ResponseEntity;
1317
import org.springframework.security.access.prepost.PreAuthorize;
14-
import org.springframework.web.bind.annotation.GetMapping;
15-
import org.springframework.web.bind.annotation.RequestMapping;
16-
import org.springframework.web.bind.annotation.RequestParam;
17-
import org.springframework.web.bind.annotation.RestController;
18+
import org.springframework.web.bind.annotation.*;
19+
20+
import java.util.List;
21+
import java.util.Optional;
1822

1923

2024
/**
@@ -25,6 +29,7 @@
2529
*/
2630
@RestController
2731
@RequestMapping("/api")
32+
@CrossOrigin(origins = "http://localhost:3000")
2833
public class UserController {
2934

3035
@Autowired
@@ -39,19 +44,61 @@ public class UserController {
3944
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
4045

4146
@GetMapping("/user/me")
42-
@PreAuthorize("hasRole('USER'")")
47+
@PreAuthorize("hasRole('USER')")
4348
public UserSummary getCurrentUser(@CurrentUser UserPrincipal currentUser)
4449
{
4550
UserSummary userSummary = new UserSummary(currentUser.getId(), currentUser.getUsername(),currentUser.getSurname(), currentUser.getName(), currentUser.getLastname(), currentUser.getCity());
4651
return userSummary;
4752
}
4853

49-
public UserIdentityAvailability checkUsernameAvailability(@RequestParam(value = "username") String username){
54+
@GetMapping("/user/checkUsernameAvailability")
55+
public UserIdentityAvailability checkUsernameAvailability(@RequestParam(value = "username") String username)
56+
{
5057
Boolean isAvailable = !userRepository.existsByUsername(username);
5158
return new UserIdentityAvailability(isAvailable);
5259
}
5360

61+
@GetMapping("/user/checkEmailAvialability")
62+
public UserIdentityAvailability checkEmailAvailability(@RequestParam(value = "email") String email)
63+
{
64+
65+
Boolean isAvailable = !userRepository.existsByEmail(email);
66+
return new UserIdentityAvailability(isAvailable);
67+
}
68+
69+
@PostMapping("/users/getBySearch")
70+
public Optional<User> getUserBySearch(@RequestParam(value = "username") String username)
71+
{
72+
return userRepository.findByUsername(username);
73+
}
74+
75+
@GetMapping("/users/getAllUsers")
76+
public List<String> getAllUsers(@PathVariable(value = "id") Long id)
77+
{
78+
return userRepository.findAllUsers();
79+
}
80+
81+
@GetMapping("/users/{id}")
82+
public UserProfile getUserProfile(@PathVariable(value = "id") Long id)
83+
{
84+
User user = userRepository.findById(id)
85+
.orElseThrow(() -> new ResourceNotFoundException("User", "username", id));
86+
UserProfile userProfile = new UserProfile(user.getId(), user.getSurname(), user.getName(), user.getLastname(), user.getUsername(), user.getCreatedAt(), user.getPhone(), user.getEmail(), user.getPassword(), user.getCity());
87+
userUtils.initUserAvatar(userProfile, user);
88+
return userProfile;
89+
}
5490

91+
@PostMapping("/users/saveUserProfile")
92+
public ResponseEntity<?> saveUserProfile(@CurrentUser UserPrincipal userPrincipal, @RequestBody UserProfile userProfileForm)
93+
{
94+
return userService.saveUserProfile(userPrincipal, userProfileForm);
95+
}
96+
97+
@PostMapping("/users/saveCity")
98+
public Boolean saveCity(@CurrentUser UserPrincipal userPrincipal, @RequestBody String city)
99+
{
100+
return userService.saveCity(userPrincipal, city);
101+
}
55102

56103

57104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.urunov.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
/**
7+
* Created by:
8+
* User: hamdamboy
9+
* Project: IntelliJ IDEA
10+
* Github: @urunov
11+
*/
12+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
13+
public class AppException extends RuntimeException
14+
{
15+
public AppException(String message){
16+
super(message);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.urunov.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
/**
7+
* Created by:
8+
* User: hamdamboy
9+
* Project: IntelliJ IDEA
10+
* Github: @urunov
11+
*/
12+
13+
@ResponseStatus(HttpStatus.BAD_REQUEST)
14+
public class BadRequestException extends RuntimeException{
15+
16+
public BadRequestException(String message){
17+
super(message);
18+
}
19+
20+
public BadRequestException(String message, Throwable cause){
21+
super(message, cause);
22+
}
23+
}

0 commit comments

Comments
 (0)