├── system.properties ├── src ├── main │ ├── java │ │ └── com │ │ │ └── pyropy │ │ │ └── usereats │ │ │ ├── service │ │ │ ├── .gitkeep │ │ │ ├── UserNotificationService.java │ │ │ ├── UserSecurityDetailsService.java │ │ │ ├── JwtService.java │ │ │ ├── UserService.java │ │ │ ├── FoodArticleOrderService.java │ │ │ ├── FoodArticleService.java │ │ │ ├── RestaurantService.java │ │ │ ├── SendGridEmailService.java │ │ │ └── OrderService.java │ │ │ ├── model │ │ │ ├── OrderStatus.java │ │ │ ├── Role.java │ │ │ ├── FoodArticleOrder.java │ │ │ ├── Order.java │ │ │ ├── Restaurant.java │ │ │ ├── FoodArticle.java │ │ │ └── User.java │ │ │ ├── repository │ │ │ ├── RoleRepository.java │ │ │ ├── UserRepository.java │ │ │ ├── FoodArticleOrderRepository.java │ │ │ ├── FoodArticleRepository.java │ │ │ ├── RestaurantRepository.java │ │ │ └── OrderRepository.java │ │ │ ├── UsereatsApplication.java │ │ │ ├── dto │ │ │ ├── NotificationDto.java │ │ │ ├── RestaurantDto.java │ │ │ ├── OrderFoodArticleDto.java │ │ │ ├── FoodArticleDto.java │ │ │ ├── OrderDto.java │ │ │ └── UserDto.java │ │ │ ├── config │ │ │ ├── PasswordConfig.java │ │ │ ├── SendGridClient.java │ │ │ ├── JwtSecretKey.java │ │ │ ├── WebConfig.java │ │ │ ├── SendGridConfig.java │ │ │ └── JwtConfig.java │ │ │ ├── converter │ │ │ └── StringToEnumConverter.java │ │ │ ├── jwt │ │ │ ├── UsernamePasswordAuthRequest.java │ │ │ ├── JwtTokenVerifier.java │ │ │ └── JwtUsernameAndPasswordAuthFilter.java │ │ │ ├── exception │ │ │ └── GlobalControllerExceptionHandler.java │ │ │ ├── api │ │ │ ├── UserController.java │ │ │ ├── FoodArticleController.java │ │ │ ├── RestaurantController.java │ │ │ └── OrderController.java │ │ │ └── security │ │ │ ├── UserSecurityDetails.java │ │ │ └── SecurityConfiguration.java │ └── resources │ │ ├── application-default.properties │ │ └── application-heroku.properties └── test │ └── java │ └── com │ └── pyropy │ └── usereats │ └── UsereatsApplicationTests.java ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ ├── maven-wrapper.properties │ └── MavenWrapperDownloader.java ├── .gitignore ├── docker-compose.yaml ├── README.md ├── pom.xml ├── mvnw.cmd └── mvnw /system.properties: -------------------------------------------------------------------------------- 1 | java.runtime.version=11 -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyropy/user-eats-rest-api/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/OrderStatus.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | public enum OrderStatus { 4 | OPEN, CONFIRMED, CANCELED; 5 | } 6 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/UserNotificationService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.dto.NotificationDto; 4 | import com.pyropy.usereats.model.User; 5 | 6 | public interface UserNotificationService { 7 | void notifyUser(User from, User to, NotificationDto notificationDto); 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/com/pyropy/usereats/UsereatsApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class UsereatsApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/repository/RoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.repository; 2 | 3 | import com.pyropy.usereats.model.Role; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface RoleRepository extends CrudRepository { 9 | Role findRoleByName(String name); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.repository; 2 | 3 | import com.pyropy.usereats.model.User; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface UserRepository extends CrudRepository { 9 | User findByUsername(String username); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/UsereatsApplication.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class UsereatsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(UsereatsApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/dto/NotificationDto.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.dto; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | // todo: attachment 7 | @Getter 8 | @Setter 9 | public class NotificationDto { 10 | private String subject; 11 | private String content; 12 | 13 | public NotificationDto(String subject, String content) { 14 | this.subject = subject; 15 | this.content = content; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/dto/RestaurantDto.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class RestaurantDto { 10 | 11 | @JsonProperty 12 | private Long id; 13 | 14 | @JsonProperty 15 | private String name; 16 | 17 | @JsonProperty 18 | private String address; 19 | 20 | @JsonProperty 21 | private String description; 22 | } 23 | -------------------------------------------------------------------------------- /src/main/resources/application-default.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.hibernate.ddl-auto=update 2 | spring.datasource.url=jdbc:mysql://localhost/db 3 | spring.datasource.username=user 4 | spring.datasource.password=password 5 | application.jwt.secretKey=reallysupersecrettokenthativemadeforyou 6 | application.jwt.tokenPrefix=Bearer 7 | application.jwt.tokenExpirationAfterDays=10 8 | application.sendgrid.apiKey=sendgridapikey 9 | application.sendgrid.replyEmail=admin@usereats.local 10 | application.sendgrid.defaultSenderEmail=admin@usereats.local 11 | 12 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.5' 2 | 3 | services: 4 | database: 5 | image: mysql:5.7 6 | container_name: db 7 | environment: 8 | MYSQL_DATABASE: 'db' 9 | MYSQL_USER: 'user' 10 | MYSQL_PASSWORD: 'password' 11 | MYSQL_ROOT_PASSWORD: 'password' 12 | ports: 13 | - '3306:3306' 14 | expose: 15 | - '3306' 16 | volumes: 17 | - db:/var/lib/mysql 18 | networks: 19 | - usereats 20 | 21 | networks: 22 | usereats: 23 | driver: bridge 24 | name: usereats 25 | 26 | volumes: 27 | db: 28 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/config/PasswordConfig.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class PasswordConfig { 10 | 11 | @Bean 12 | public PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(10); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/repository/FoodArticleOrderRepository.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.repository; 2 | 3 | import com.pyropy.usereats.model.FoodArticleOrder; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Optional; 8 | 9 | @Repository 10 | public interface FoodArticleOrderRepository extends CrudRepository { 11 | Optional findFoodArticleOrderByFoodArticleIdAndOrderId(Long foodArticleId, Long orderId); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/config/SendGridClient.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.config; 2 | 3 | import com.sendgrid.SendGrid; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class SendGridClient { 10 | 11 | @Autowired 12 | private SendGridConfig sendGridConfig; 13 | 14 | @Bean 15 | public SendGrid getSendGrid() { 16 | return new SendGrid(sendGridConfig.getApiKey()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # User Eats REST API 2 | 3 | **REST API for food ordering example app.** 4 | 5 | ## Getting Started 6 | 7 | ## Prerequisite 8 | 9 | ## Installing 10 | 11 | ## Running the tests 12 | 13 | ## Built with 14 | 15 | * Spring Boot 16 | * Spring Data 17 | * Spring Security 18 | * Maven 19 | * OpenJDK 11 20 | 21 | ## Contributing 22 | 23 | Click [here](https://github.com/pyropy/user-eats-rest-api/graphs/contributors) to see list of contributors. 24 | 25 | ## Authors 26 | 27 | * Srdjan Stankovic - Project Owner - [GitHub](https://github.com/pyropy) 28 | -------------------------------------------------------------------------------- /src/main/resources/application-heroku.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.hibernate.ddl-auto=update 2 | spring.datasource.url=${CLEARDB_DATABASE_URL} 3 | spring.datasource.username=${CLEARDB_USER} 4 | spring.datasource.password=${CLEARDB_PASS} 5 | application.jwt.secretKey=${JWT_SECRET_KEY} 6 | application.jwt.tokenPrefix=${JWT_TOKEN_PREFIX} 7 | application.jwt.tokenExpirationAfterDays=${JWT_TOKEN_EXP_AFTER_DAYS} 8 | application.sendgrid.apiKey=${SENDGRID_API_KEY} 9 | application.sendgrid.replyEmail=${DEFAULT_REPLY_EMAIL} 10 | application.sendgrid.defaultSenderEmail=${DEFAULT_SENDER_EMAIL} 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/converter/StringToEnumConverter.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.converter; 2 | 3 | import com.pyropy.usereats.model.OrderStatus; 4 | import org.springframework.core.convert.converter.Converter; 5 | 6 | public class StringToEnumConverter implements Converter { 7 | 8 | @Override 9 | public OrderStatus convert(String source) { 10 | try { 11 | return OrderStatus.valueOf(source.toUpperCase()); 12 | } catch (IllegalArgumentException err) { 13 | return null; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/dto/OrderFoodArticleDto.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class OrderFoodArticleDto { 10 | 11 | @JsonProperty 12 | private Long foodArticleId; 13 | 14 | @JsonProperty(access = JsonProperty.Access.READ_ONLY) 15 | private String foodArticleName; 16 | 17 | @JsonProperty 18 | private Integer quantity; 19 | 20 | @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) 21 | private Long orderId; 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/dto/FoodArticleDto.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class FoodArticleDto { 10 | 11 | @JsonProperty 12 | private Long id; 13 | 14 | @JsonProperty 15 | private String name; 16 | 17 | @JsonProperty 18 | private Float price; 19 | 20 | @JsonProperty 21 | private String imageUrl; 22 | 23 | @JsonProperty 24 | private String description; 25 | 26 | @JsonProperty 27 | private Long restaurantId; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/repository/FoodArticleRepository.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.repository; 2 | 3 | import com.pyropy.usereats.model.FoodArticle; 4 | import org.springframework.data.repository.PagingAndSortingRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | import java.util.Optional; 9 | 10 | @Repository 11 | public interface FoodArticleRepository extends PagingAndSortingRepository { 12 | List findFoodArticleByRestaurantId(Long Id); 13 | 14 | Optional findFoodArticleByIdAndRestaurantOwnerUsername(Long id, String username); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/repository/RestaurantRepository.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.repository; 2 | 3 | import com.pyropy.usereats.model.Restaurant; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Optional; 8 | 9 | @Repository 10 | public interface RestaurantRepository extends CrudRepository { 11 | Iterable findByNameLike(String name); 12 | 13 | Iterable findRestaurantByOwnerUsername(String username); 14 | 15 | Optional findRestaurantByIdAndOwnerUsername(Long id, String username); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/jwt/UsernamePasswordAuthRequest.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.jwt; 2 | 3 | public class UsernamePasswordAuthRequest { 4 | 5 | private String username; 6 | private String password; 7 | 8 | 9 | public UsernamePasswordAuthRequest() { 10 | } 11 | 12 | public String getUsername() { 13 | return username; 14 | } 15 | 16 | public void setUsername(String username) { 17 | this.username = username; 18 | } 19 | 20 | public String getPassword() { 21 | return password; 22 | } 23 | 24 | public void setPassword(String password) { 25 | this.password = password; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/exception/GlobalControllerExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.exception; 2 | 3 | import org.springframework.core.convert.ConversionFailedException; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.ControllerAdvice; 7 | import org.springframework.web.bind.annotation.ExceptionHandler; 8 | 9 | @ControllerAdvice 10 | public class GlobalControllerExceptionHandler { 11 | @ExceptionHandler(ConversionFailedException.class) 12 | public ResponseEntity handleConflict(RuntimeException err) { 13 | return new ResponseEntity<>(err.getMessage(), HttpStatus.BAD_REQUEST); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/config/JwtSecretKey.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.config; 2 | 3 | import io.jsonwebtoken.security.Keys; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import javax.crypto.SecretKey; 9 | 10 | @Configuration 11 | public class JwtSecretKey { 12 | 13 | private final JwtConfig jwtConfig; 14 | 15 | @Autowired 16 | public JwtSecretKey(JwtConfig jwtConfig) { 17 | this.jwtConfig = jwtConfig; 18 | } 19 | 20 | @Bean 21 | public SecretKey secretKey() { 22 | return Keys.hmacShaKeyFor(jwtConfig.getSecretKey().getBytes()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/dto/OrderDto.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import com.pyropy.usereats.model.OrderStatus; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.List; 9 | 10 | @Getter 11 | @Setter 12 | public class OrderDto { 13 | @JsonProperty 14 | private Long id; 15 | 16 | @JsonProperty 17 | private Float subtotal; 18 | 19 | @JsonProperty 20 | private Float total; 21 | 22 | @JsonProperty(access = JsonProperty.Access.READ_ONLY) 23 | private OrderStatus status; 24 | 25 | @JsonProperty 26 | private List foodArticles; 27 | 28 | @JsonProperty 29 | private String deliveryAddress; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/repository/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.repository; 2 | 3 | import com.pyropy.usereats.model.Order; 4 | import com.pyropy.usereats.model.OrderStatus; 5 | import com.pyropy.usereats.model.User; 6 | import org.springframework.data.repository.PagingAndSortingRepository; 7 | import org.springframework.stereotype.Repository; 8 | 9 | import java.util.List; 10 | import java.util.Optional; 11 | 12 | @Repository 13 | public interface OrderRepository extends PagingAndSortingRepository { 14 | Optional findOrderByUserAndOrderStatus(User user, OrderStatus status); 15 | 16 | Optional findOrderByIdAndUserUsername(Long id, String username); 17 | 18 | List findOrderByUserUsername(String username); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/config/WebConfig.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.config; 2 | 3 | import com.pyropy.usereats.converter.StringToEnumConverter; 4 | import org.modelmapper.ModelMapper; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.format.FormatterRegistry; 8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 9 | 10 | @Configuration 11 | public class WebConfig implements WebMvcConfigurer { 12 | 13 | @Bean 14 | public ModelMapper modelMapper() { 15 | return new ModelMapper(); 16 | } 17 | 18 | @Override 19 | public void addFormatters(FormatterRegistry registry) { 20 | registry.addConverter(new StringToEnumConverter()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/dto/UserDto.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.UUID; 9 | 10 | @Getter 11 | @Setter 12 | public class UserDto { 13 | 14 | @JsonIgnore 15 | private Long id; 16 | 17 | @JsonProperty 18 | private String email; 19 | 20 | @JsonProperty 21 | private String username; 22 | 23 | @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) 24 | private String password; 25 | 26 | @JsonProperty 27 | private String firstName; 28 | 29 | @JsonProperty 30 | private String lastName; 31 | 32 | @JsonProperty 33 | private String address; 34 | 35 | @JsonProperty 36 | private boolean isRestaurantAdmin; 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/Role.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | import javax.persistence.*; 4 | import java.util.Objects; 5 | 6 | @Entity 7 | public class Role { 8 | 9 | @Id 10 | @Column(length = 50, nullable = false, name = "NAME") 11 | private String name; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | @Override 22 | public boolean equals(Object o) { 23 | if (this == o) return true; 24 | if (o == null || getClass() != o.getClass()) return false; 25 | Role role = (Role) o; 26 | return name == role.name; 27 | } 28 | 29 | @Override 30 | public int hashCode() { 31 | return Objects.hash(name); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "Role{" + 37 | "name=" + name + 38 | '}'; 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/FoodArticleOrder.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import javax.persistence.*; 7 | 8 | @Getter 9 | @Setter 10 | @Entity 11 | @Table(name = "FOOD_ARTICLE_ORDER") 12 | public class FoodArticleOrder { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.AUTO) 16 | @Column(name = "ID", updatable = false, nullable = false) 17 | private Long id; 18 | 19 | @ManyToOne 20 | @JoinColumn(name = "ORDERS_ID", nullable = false) 21 | private Order order; 22 | 23 | @ManyToOne(fetch = FetchType.EAGER) 24 | @JoinColumn(name = "FOOD_ARTICLE_ID", nullable = false) 25 | private FoodArticle foodArticle; 26 | 27 | @Column 28 | private Integer quantity; 29 | 30 | public FoodArticleOrder() { 31 | } 32 | 33 | public FoodArticleOrder(FoodArticle foodArticle, Order order, Integer quantity) { 34 | this.order = order; 35 | this.foodArticle = foodArticle; 36 | this.quantity = quantity; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/api/UserController.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.api; 2 | 3 | import com.pyropy.usereats.dto.UserDto; 4 | import com.pyropy.usereats.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import java.util.List; 10 | 11 | 12 | @RestController 13 | @RequestMapping("/api/v1/users") 14 | public class UserController { 15 | 16 | @Autowired 17 | UserService userService; 18 | 19 | @GetMapping 20 | public List getAllUsers() { 21 | return userService.findAll(); 22 | } 23 | 24 | @GetMapping(value = "{username}") 25 | public UserDto getUserByUsername(@PathVariable("username") String username) { 26 | return userService.findByUsername(username); 27 | } 28 | 29 | /* 30 | * Endpoint used for registering users. 31 | */ 32 | @PostMapping 33 | @ResponseStatus(HttpStatus.CREATED) 34 | public UserDto createUser(@RequestBody UserDto user) { 35 | return userService.createUser(user); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/config/SendGridConfig.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | 7 | @Configuration 8 | @ConfigurationProperties(prefix = "application.sendgrid") 9 | public class SendGridConfig { 10 | private String apiKey; 11 | private String replyEmail; 12 | private String defaultSenderEmail; 13 | 14 | public SendGridConfig() { 15 | } 16 | 17 | public String getReplyEmail() { 18 | return replyEmail; 19 | } 20 | 21 | public String getDefaultSenderEmail() { 22 | return defaultSenderEmail; 23 | } 24 | 25 | public void setReplyEmail(String replyEmail) { 26 | this.replyEmail = replyEmail; 27 | } 28 | 29 | public void setDefaultSenderEmail(String defaultSenderEmail) { 30 | this.defaultSenderEmail = defaultSenderEmail; 31 | } 32 | 33 | public String getApiKey() { 34 | return apiKey; 35 | } 36 | 37 | public void setApiKey(String apiKey) { 38 | this.apiKey = apiKey; 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/UserSecurityDetailsService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.model.User; 4 | import com.pyropy.usereats.repository.UserRepository; 5 | import com.pyropy.usereats.security.UserSecurityDetails; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.security.core.userdetails.UserDetails; 8 | import org.springframework.security.core.userdetails.UserDetailsService; 9 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 10 | import org.springframework.stereotype.Service; 11 | 12 | 13 | @Service 14 | public class UserSecurityDetailsService implements UserDetailsService { 15 | 16 | private final UserRepository userRepository; 17 | 18 | @Autowired 19 | public UserSecurityDetailsService(UserRepository userRepository) { 20 | this.userRepository = userRepository; 21 | } 22 | 23 | @Override 24 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 25 | User user = userRepository.findByUsername(username); 26 | return new UserSecurityDetails(user); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/config/JwtConfig.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.config; 2 | 3 | 4 | import com.google.common.net.HttpHeaders; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | 9 | @Configuration 10 | @ConfigurationProperties(prefix = "application.jwt") 11 | public class JwtConfig { 12 | 13 | private String secretKey; 14 | private String tokenPrefix; 15 | private Integer tokenExpirationAfterDays; 16 | 17 | public JwtConfig() { 18 | } 19 | 20 | public String getSecretKey() { 21 | return secretKey; 22 | } 23 | 24 | public void setSecretKey(String secretKey) { 25 | this.secretKey = secretKey; 26 | } 27 | 28 | public String getTokenPrefix() { 29 | return tokenPrefix; 30 | } 31 | 32 | public void setTokenPrefix(String tokenPrefix) { 33 | this.tokenPrefix = tokenPrefix; 34 | } 35 | 36 | public Integer getTokenExpirationAfterDays() { 37 | return tokenExpirationAfterDays; 38 | } 39 | 40 | public void setTokenExpirationAfterDays(Integer tokenExpirationAfterDays) { 41 | this.tokenExpirationAfterDays = tokenExpirationAfterDays; 42 | } 43 | 44 | public String getAuthorizationHeader() { 45 | return HttpHeaders.AUTHORIZATION; 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/security/UserSecurityDetails.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.security; 2 | 3 | import com.pyropy.usereats.model.User; 4 | import org.springframework.security.core.GrantedAuthority; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | 7 | import java.util.Collection; 8 | 9 | public class UserSecurityDetails implements UserDetails { 10 | private User user; 11 | 12 | public UserSecurityDetails(User user) { 13 | this.user = user; 14 | } 15 | 16 | // todo: implement UserDetails methods 17 | @Override 18 | public Collection getAuthorities() { 19 | return user.getAuthorities(); 20 | } 21 | 22 | @Override 23 | public String getPassword() { 24 | return user.getPassword(); 25 | } 26 | 27 | @Override 28 | public String getUsername() { 29 | return user.getUsername(); 30 | } 31 | 32 | @Override 33 | public boolean isAccountNonExpired() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isAccountNonLocked() { 39 | return true; 40 | } 41 | 42 | @Override 43 | public boolean isCredentialsNonExpired() { 44 | return true; 45 | } 46 | 47 | @Override 48 | public boolean isEnabled() { 49 | return user.isActivated(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/Order.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.hibernate.annotations.OnDelete; 6 | import org.hibernate.annotations.OnDeleteAction; 7 | 8 | import javax.persistence.*; 9 | import java.util.HashSet; 10 | import java.util.Set; 11 | 12 | 13 | @Getter 14 | @Setter 15 | @Entity 16 | @Table(name = "ORDERS") 17 | public class Order { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.AUTO) 21 | @Column(name = "ID", nullable = false, updatable = false) 22 | private Long id; 23 | 24 | @Column(nullable = false, name = "SUBTOTAL") 25 | private Float subtotal = (float) 0; 26 | 27 | @Column(nullable = false, name = "TOTAL") 28 | private Float total = (float) 0; 29 | 30 | @Column(nullable = false, name = "STATUS") 31 | @Enumerated(EnumType.ORDINAL) 32 | private OrderStatus orderStatus; 33 | 34 | @Column(name = "DELIVERY_ADDRESS") 35 | private String deliveryAddress; 36 | 37 | @ManyToOne(fetch = FetchType.LAZY, optional = false) 38 | @OnDelete(action = OnDeleteAction.CASCADE) 39 | private User user; 40 | 41 | @OneToMany(mappedBy = "order") 42 | private Set foodArticles = new HashSet<>(); 43 | 44 | public Order() { 45 | } 46 | 47 | public Order(User user) { 48 | this.orderStatus = OrderStatus.OPEN; 49 | this.user = user; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/JwtService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.config.JwtConfig; 4 | import io.jsonwebtoken.Claims; 5 | import io.jsonwebtoken.Jwts; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.crypto.SecretKey; 10 | import java.util.function.Function; 11 | 12 | @Service 13 | public class JwtService { 14 | 15 | private final JwtConfig jwtConfig; 16 | private final SecretKey secretKey; 17 | 18 | @Autowired 19 | public JwtService(JwtConfig jwtConfig, SecretKey secretKey) { 20 | this.jwtConfig = jwtConfig; 21 | this.secretKey = secretKey; 22 | } 23 | 24 | public String getUsernameFromToken(String token) { 25 | return getClaimFromToken(token, Claims::getSubject); 26 | } 27 | 28 | public T getClaimFromToken(String token, Function claimsResolver) { 29 | Claims claims = getClaimsFromToken(token); 30 | return claimsResolver.apply(claims); 31 | } 32 | 33 | public Claims getClaimsFromToken(String token) { 34 | return Jwts.parser() 35 | .setSigningKey(secretKey) 36 | .parseClaimsJws(removeTokenPrefix(token)) 37 | .getBody(); 38 | } 39 | 40 | public String removeTokenPrefix(String token) { 41 | return token.replace(jwtConfig.getTokenPrefix(), ""); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/Restaurant.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.hibernate.annotations.OnDelete; 6 | import org.hibernate.annotations.OnDeleteAction; 7 | 8 | import javax.persistence.*; 9 | 10 | @Getter 11 | @Setter 12 | @Entity 13 | @Table(name = "RESTURAUNT") 14 | public class Restaurant { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | @Column(name = "ID", nullable = false, updatable = false) 19 | private Long id; 20 | 21 | @Column(nullable = false, length = 100, name = "NAME") 22 | private String name; 23 | 24 | @Column(nullable = false, name = "ADDRESS") 25 | private String address; 26 | 27 | @Column(name = "DESCRIPTION") 28 | private String description; 29 | 30 | @Column(nullable = false) 31 | private boolean active; 32 | 33 | @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE, optional = false) 34 | @JoinColumn(name = "USER_ID") 35 | @OnDelete(action = OnDeleteAction.CASCADE) 36 | private User owner; 37 | 38 | public Restaurant() { 39 | } 40 | 41 | public Restaurant(String name, String description, String address, User owner) { 42 | this.name = name; 43 | this.description = description; 44 | this.address = address; 45 | this.owner = owner; 46 | this.active = true; // todo: update changing restaurant status 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/FoodArticle.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.hibernate.annotations.OnDelete; 6 | import org.hibernate.annotations.OnDeleteAction; 7 | 8 | import javax.persistence.*; 9 | 10 | @Getter 11 | @Setter 12 | @Entity 13 | @Table(name = "FOOD_ARTICLE") 14 | public class FoodArticle { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.AUTO) 18 | @Column(name = "ID", nullable = false, updatable = false) 19 | private Long id; 20 | 21 | @Column(nullable = false, length = 100, name = "NAME") 22 | private String name; 23 | 24 | @Column(nullable = false, name = "PRICE") 25 | private Float price = (float) 0.0; 26 | 27 | @Column(nullable = false, name = "IMAGE_URL") 28 | private String imageUrl; 29 | 30 | @Column(name = "DESCRIPTION") 31 | private String description; 32 | 33 | @Column(nullable = false) 34 | private boolean active = true; 35 | 36 | @ManyToOne(fetch = FetchType.LAZY, optional = false) 37 | @JoinColumn(name = "RESTURAUNT_ID", nullable = false) 38 | @OnDelete(action = OnDeleteAction.CASCADE) 39 | private Restaurant restaurant; 40 | 41 | public FoodArticle() { 42 | } 43 | 44 | public FoodArticle(String name, String description, Float price, String imageUrl, Restaurant restaurant) { 45 | this.name = name; 46 | this.description = description; 47 | this.price = price; 48 | this.restaurant = restaurant; 49 | this.imageUrl = imageUrl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/api/FoodArticleController.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.api; 2 | 3 | import com.pyropy.usereats.dto.FoodArticleDto; 4 | import com.pyropy.usereats.service.FoodArticleService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.security.core.Authentication; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import javax.persistence.EntityNotFoundException; 10 | import javax.websocket.server.PathParam; 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/api/v1/food") 15 | public class FoodArticleController { 16 | 17 | @Autowired 18 | private FoodArticleService foodArticleService; 19 | 20 | @GetMapping 21 | public List getAllFoodArticles() { 22 | // todo: add pagination 23 | return foodArticleService.findAll(); 24 | } 25 | 26 | @GetMapping(value = "{restaurantId}") 27 | public List getRestaurantFoodArticles(@PathVariable("restaurantId") Long restaurantId) { 28 | return foodArticleService.findFoodArticlesByRestaurantId(restaurantId); 29 | } 30 | 31 | @PostMapping 32 | public FoodArticleDto createFoodArticle(Authentication authentication, 33 | @RequestBody FoodArticleDto foodArticleDto) { 34 | return foodArticleService.createFoodArticle(foodArticleDto, authentication.getName()); 35 | } 36 | 37 | @PutMapping 38 | public FoodArticleDto updateFoodArticle(Authentication authentication, 39 | @RequestBody FoodArticleDto foodArticleInfo) { 40 | return foodArticleService.updateFoodArticle(foodArticleInfo, authentication.getName()); 41 | } 42 | 43 | @DeleteMapping 44 | public void deleteFoodArticle(Authentication authentication, 45 | @RequestBody FoodArticleDto foodArticleInfo) { 46 | foodArticleService.deleteFoodArticle(foodArticleInfo, authentication.getName()); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/api/RestaurantController.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.api; 2 | 3 | import com.pyropy.usereats.dto.RestaurantDto; 4 | import com.pyropy.usereats.service.RestaurantService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.security.access.annotation.Secured; 8 | import org.springframework.security.core.Authentication; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.websocket.server.PathParam; 12 | import java.util.List; 13 | 14 | @RestController 15 | @RequestMapping("/api/v1/restaurants") 16 | public class RestaurantController { 17 | 18 | @Autowired 19 | private RestaurantService restaurantService; 20 | 21 | @GetMapping 22 | public List getAllRestaurants() { 23 | return restaurantService.findAll(); 24 | } 25 | 26 | @GetMapping(value = "{name}") 27 | public List getResturauntsByName(@PathVariable("name") String name) { 28 | return restaurantService.findByNameLike(name); 29 | } 30 | 31 | @GetMapping(path = "me") 32 | @Secured(value = "ROLE_RESTAURANT_ADMIN") 33 | public List getUserRestaurants(Authentication authentication) { 34 | return restaurantService.findByOwnerUsername(authentication.getName()); 35 | } 36 | 37 | @PostMapping 38 | @ResponseStatus(HttpStatus.CREATED) 39 | public RestaurantDto createRestaurant(Authentication authentication, @RequestBody RestaurantDto restaurantInfo) { 40 | return restaurantService.createRestaurant(restaurantInfo, authentication.getName()); 41 | } 42 | 43 | @PutMapping 44 | public RestaurantDto updateRestaurant(Authentication authentication, @RequestBody RestaurantDto restaurantInfo) { 45 | return restaurantService.updateRestaurant(restaurantInfo, authentication.getName()); 46 | } 47 | 48 | @DeleteMapping 49 | public void deleteRestaurant(Authentication authentication, @RequestBody RestaurantDto restaurantInfo) { 50 | restaurantService.deleteRestaurant(restaurantInfo, authentication.getName()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.pyropy.usereats.dto.UserDto; 5 | import com.pyropy.usereats.model.User; 6 | import com.pyropy.usereats.repository.RoleRepository; 7 | import com.pyropy.usereats.repository.UserRepository; 8 | import org.modelmapper.ModelMapper; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.security.crypto.password.PasswordEncoder; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.util.Arrays; 14 | import java.util.List; 15 | import java.util.stream.Collectors; 16 | import java.util.stream.StreamSupport; 17 | 18 | 19 | @Service 20 | public class UserService { 21 | 22 | @Autowired 23 | private UserRepository userRepository; 24 | 25 | @Autowired 26 | private PasswordEncoder passwordEncoder; 27 | 28 | @Autowired 29 | private RoleRepository roleRepository; 30 | 31 | @Autowired 32 | private ModelMapper modelMapper; 33 | 34 | /* 35 | * Converts User Entity to UserDto class. 36 | * */ 37 | public UserDto convertToDto(User user) { 38 | return modelMapper.map(user, UserDto.class); 39 | } 40 | 41 | /* 42 | * Converts UserDto to User Entity class. 43 | * */ 44 | public User convertToEntity(UserDto userDto) { 45 | return modelMapper.map(userDto, User.class); 46 | } 47 | 48 | 49 | /* 50 | * Encrypts password and persists User to Database. 51 | */ 52 | public UserDto createUser(UserDto userInfo) { 53 | User user = convertToEntity(userInfo); 54 | user.setPassword(passwordEncoder.encode(user.getPassword())); 55 | if (userInfo.isRestaurantAdmin()) { 56 | setResturantAdminRole(user); 57 | } 58 | userRepository.save(user); 59 | return userInfo; 60 | } 61 | 62 | public UserDto findByUsername(String username) { 63 | User user = userRepository.findByUsername(username); 64 | return convertToDto(user); 65 | } 66 | 67 | public List findAll() { 68 | Iterable users = userRepository.findAll(); 69 | return StreamSupport 70 | .stream(users.spliterator(), false) 71 | .map(this::convertToDto) 72 | .collect(Collectors.toList()); 73 | } 74 | 75 | public User setResturantAdminRole(User user) { 76 | user.setRoles(Arrays.asList(roleRepository.findRoleByName("ROLE_RESTAURANT_ADMIN"))); 77 | return user; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/api/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.api; 2 | 3 | import com.pyropy.usereats.dto.OrderFoodArticleDto; 4 | import com.pyropy.usereats.dto.OrderDto; 5 | import com.pyropy.usereats.model.*; 6 | import com.pyropy.usereats.service.OrderService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.security.core.Authentication; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.websocket.server.PathParam; 12 | import java.util.List; 13 | 14 | 15 | @RestController 16 | @RequestMapping("/api/v1/order") 17 | public class OrderController { 18 | 19 | @Autowired 20 | private OrderService orderService; 21 | 22 | /* Get user order by order id */ 23 | @GetMapping(value = "{id}") 24 | public OrderDto getUserOrder(Authentication authentication, 25 | @PathVariable Long id) { 26 | return orderService.findOrderByIdAndUsername(id, authentication.getName()); 27 | } 28 | 29 | /* Get all user orders */ 30 | @GetMapping(path = "me/all") 31 | public List getUserOrders(Authentication authentication) { 32 | return orderService.findUserOrders(authentication.getName()); 33 | } 34 | 35 | 36 | /* Create new order */ 37 | @PostMapping 38 | public OrderDto createOrder(Authentication authentication, 39 | @RequestBody OrderFoodArticleDto articles) { 40 | return orderService.createOrder(articles, authentication.getName()); 41 | } 42 | 43 | /* Update order articles */ 44 | @PutMapping 45 | public OrderDto updateOrderFoodArticles(Authentication authentication, 46 | @RequestBody OrderFoodArticleDto orderFoodArticleDto) { 47 | return orderService.updateOrderArticles(orderFoodArticleDto, authentication.getName()); 48 | } 49 | 50 | /* Update order articles */ 51 | @DeleteMapping 52 | public OrderDto deleteOrderFoodArticles(Authentication authentication, 53 | @RequestBody OrderFoodArticleDto orderFoodArticleDto) { 54 | return orderService.deleteOrderArticle(orderFoodArticleDto, authentication.getName()); 55 | } 56 | 57 | /* Update order status */ 58 | @PutMapping(value = "{status}") 59 | public OrderDto updateOrderStatus(Authentication authentication, 60 | @RequestBody OrderDto orderDto, 61 | @PathVariable OrderStatus status) { 62 | return orderService.updateOrderStatus(orderDto, status, authentication.getName()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/model/User.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.model; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.hibernate.annotations.BatchSize; 6 | import org.springframework.security.core.GrantedAuthority; 7 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 8 | 9 | import javax.persistence.*; 10 | import java.util.*; 11 | 12 | @Getter 13 | @Setter 14 | @Entity 15 | @Table(name = "USER") 16 | public class User { 17 | 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | @Column(name = "ID", nullable = false, updatable = false) 22 | private Long id; 23 | 24 | @Column(nullable = false, unique = true, length = 100, name = "EMAIL") 25 | private String email; 26 | 27 | @Column(nullable = false, unique = true, length = 50, name = "USERNAME") 28 | private String username; 29 | 30 | @Column(length = 100, name = "PASSWORD") 31 | private String password; 32 | 33 | @Column(length = 50, nullable = false, name = "FIRST_NAME") 34 | private String firstName; 35 | 36 | @Column(length = 50, nullable = false, name = "LAST_NAME") 37 | private String lastName; 38 | 39 | @Column(nullable = false, name = "ADDRESS") 40 | private String address; 41 | 42 | @Column(nullable = false, name = "ACTIVATED") 43 | private boolean activated = true; // todo: implement email confirmation 44 | 45 | @ManyToMany(fetch = FetchType.EAGER) 46 | @JoinTable( 47 | name = "USER_ROLE", 48 | joinColumns = {@JoinColumn(name = "USER_ID", referencedColumnName = "ID")}, 49 | inverseJoinColumns = {@JoinColumn(name = "ROLE_NAME", referencedColumnName = "NAME")}) 50 | @BatchSize(size = 20) 51 | private List roles = new ArrayList<>(); 52 | 53 | @OneToMany(fetch = FetchType.LAZY) 54 | @JoinColumn(name = "ORDERS_ID") 55 | private List orders; 56 | 57 | public User() { 58 | } 59 | 60 | public User(String firstName, 61 | String lastName, 62 | String address, 63 | String password, 64 | String email, 65 | String username) { 66 | this.firstName = firstName; 67 | this.lastName = lastName; 68 | this.address = address; 69 | this.password = password; 70 | this.email = email; 71 | this.username = username; 72 | } 73 | 74 | public Collection getAuthorities() { 75 | List authorities = new ArrayList<>(); 76 | roles.forEach(r -> { 77 | authorities.add(new SimpleGrantedAuthority(r.getName())); 78 | }); 79 | return authorities; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/jwt/JwtTokenVerifier.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.jwt; 2 | 3 | import com.google.common.base.Strings; 4 | import com.pyropy.usereats.config.JwtConfig; 5 | import io.jsonwebtoken.Claims; 6 | import io.jsonwebtoken.Jws; 7 | import io.jsonwebtoken.JwtException; 8 | import io.jsonwebtoken.Jwts; 9 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 10 | import org.springframework.security.core.Authentication; 11 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 12 | import org.springframework.security.core.context.SecurityContextHolder; 13 | import org.springframework.web.filter.OncePerRequestFilter; 14 | 15 | import javax.crypto.SecretKey; 16 | import javax.servlet.FilterChain; 17 | import javax.servlet.ServletException; 18 | import javax.servlet.http.HttpServletRequest; 19 | import javax.servlet.http.HttpServletResponse; 20 | import java.io.IOException; 21 | import java.util.List; 22 | import java.util.Map; 23 | import java.util.Set; 24 | import java.util.stream.Collectors; 25 | 26 | public class JwtTokenVerifier extends OncePerRequestFilter { 27 | 28 | private final SecretKey secretKey; 29 | private final JwtConfig jwtConfig; 30 | 31 | public JwtTokenVerifier(SecretKey secretKey, 32 | JwtConfig jwtConfig) { 33 | this.secretKey = secretKey; 34 | this.jwtConfig = jwtConfig; 35 | } 36 | 37 | @Override 38 | protected void doFilterInternal(HttpServletRequest request, 39 | HttpServletResponse response, 40 | FilterChain filterChain) throws ServletException, IOException { 41 | 42 | String authorizationHeader = request.getHeader(jwtConfig.getAuthorizationHeader()); 43 | 44 | if (Strings.isNullOrEmpty(authorizationHeader) || !authorizationHeader.startsWith(jwtConfig.getTokenPrefix())) { 45 | filterChain.doFilter(request, response); 46 | return; 47 | } 48 | 49 | String token = authorizationHeader.replace(jwtConfig.getTokenPrefix(), ""); 50 | 51 | try { 52 | 53 | Jws claimsJws = Jwts.parser() 54 | .setSigningKey(secretKey) 55 | .parseClaimsJws(token); 56 | 57 | Claims body = claimsJws.getBody(); 58 | 59 | String username = body.getSubject(); 60 | 61 | var authorities = (List>) body.get("authorities"); 62 | 63 | Set simpleGrantedAuthorities = authorities.stream() 64 | .map(m -> new SimpleGrantedAuthority(m.get("authority"))) 65 | .collect(Collectors.toSet()); 66 | 67 | Authentication authentication = new UsernamePasswordAuthenticationToken( 68 | username, 69 | null, 70 | simpleGrantedAuthorities 71 | ); 72 | 73 | SecurityContextHolder.getContext().setAuthentication(authentication); 74 | 75 | } catch (JwtException e) { 76 | throw new IllegalStateException(String.format("Token %s cannot be trusted", token)); 77 | } 78 | 79 | filterChain.doFilter(request, response); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/jwt/JwtUsernameAndPasswordAuthFilter.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.jwt; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.pyropy.usereats.config.JwtConfig; 5 | import io.jsonwebtoken.Jwts; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 8 | import org.springframework.security.core.Authentication; 9 | import org.springframework.security.core.AuthenticationException; 10 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 11 | 12 | import javax.crypto.SecretKey; 13 | import javax.servlet.FilterChain; 14 | import javax.servlet.ServletException; 15 | import javax.servlet.http.HttpServletRequest; 16 | import javax.servlet.http.HttpServletResponse; 17 | import java.io.IOException; 18 | import java.time.LocalDate; 19 | import java.util.Date; 20 | 21 | public class JwtUsernameAndPasswordAuthFilter extends UsernamePasswordAuthenticationFilter { 22 | 23 | private final AuthenticationManager authenticationManager; 24 | private final JwtConfig jwtConfig; 25 | private final SecretKey secretKey; 26 | 27 | public JwtUsernameAndPasswordAuthFilter(AuthenticationManager authenticationManager, 28 | JwtConfig jwtConfig, 29 | SecretKey secretKey) { 30 | this.authenticationManager = authenticationManager; 31 | this.jwtConfig = jwtConfig; 32 | this.secretKey = secretKey; 33 | } 34 | 35 | @Override 36 | public Authentication attemptAuthentication(HttpServletRequest request, 37 | HttpServletResponse response) throws AuthenticationException { 38 | try { 39 | 40 | UsernamePasswordAuthRequest authenticationRequest = new ObjectMapper() 41 | .readValue(request.getInputStream(), UsernamePasswordAuthRequest.class); 42 | 43 | Authentication authentication = new UsernamePasswordAuthenticationToken( 44 | authenticationRequest.getUsername(), 45 | authenticationRequest.getPassword()); 46 | return authenticationManager.authenticate(authentication); 47 | } catch (IOException e) { 48 | throw new RuntimeException(e); 49 | } 50 | } 51 | 52 | @Override 53 | protected void successfulAuthentication(HttpServletRequest request, 54 | HttpServletResponse response, 55 | FilterChain chain, 56 | Authentication authResult) throws IOException, ServletException { 57 | String token = Jwts.builder() 58 | .setSubject(authResult.getName()) 59 | .claim("authorities", authResult.getAuthorities()) 60 | .setIssuedAt(new Date()) 61 | .setExpiration(java.sql.Date.valueOf(LocalDate.now().plusDays(jwtConfig.getTokenExpirationAfterDays()))) 62 | .signWith(secretKey) 63 | .compact(); 64 | 65 | response.addHeader(jwtConfig.getAuthorizationHeader(), jwtConfig.getTokenPrefix() + token); 66 | } 67 | } -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/FoodArticleOrderService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.dto.OrderFoodArticleDto; 4 | import com.pyropy.usereats.model.FoodArticle; 5 | import com.pyropy.usereats.model.FoodArticleOrder; 6 | import com.pyropy.usereats.model.Order; 7 | import com.pyropy.usereats.repository.FoodArticleOrderRepository; 8 | import org.modelmapper.ModelMapper; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.List; 13 | import java.util.Optional; 14 | import java.util.stream.Collectors; 15 | import java.util.stream.StreamSupport; 16 | 17 | @Service 18 | public class FoodArticleOrderService { 19 | 20 | @Autowired 21 | private ModelMapper modelMapper; 22 | 23 | @Autowired 24 | private FoodArticleService foodArticleService; 25 | 26 | @Autowired 27 | private OrderService orderService; 28 | 29 | @Autowired 30 | private FoodArticleOrderRepository foodArticleOrderRepository; 31 | 32 | /* 33 | * Converts OrderArticle Entity to OrderArticleDto class. 34 | * */ 35 | public OrderFoodArticleDto convertToDto(FoodArticleOrder foodArticleOrder) { 36 | return modelMapper.map(foodArticleOrder, OrderFoodArticleDto.class); 37 | } 38 | 39 | /* 40 | * Converts OrderArticleDto entity to OrderArticle class. 41 | * */ 42 | public FoodArticleOrder convertToEntity(OrderFoodArticleDto orderFoodArticleDto) { 43 | return modelMapper.map(orderFoodArticleDto, FoodArticleOrder.class); 44 | } 45 | 46 | public List convertIterableToListDto(Iterable orderArticles) { 47 | return StreamSupport 48 | .stream(orderArticles.spliterator(), false) 49 | .map(this::convertToDto) 50 | .collect(Collectors.toList()); 51 | } 52 | 53 | public OrderFoodArticleDto createFoodArticleOrder(OrderFoodArticleDto orderFoodArticleDto) { 54 | FoodArticle foodArticle = foodArticleService 55 | .convertToEntity(foodArticleService.findById(orderFoodArticleDto.getFoodArticleId())); 56 | 57 | Order order = orderService.findEntityById(orderFoodArticleDto.getOrderId()); 58 | FoodArticleOrder foodArticleOrder = new FoodArticleOrder(foodArticle, order, orderFoodArticleDto.getQuantity()); 59 | foodArticleOrderRepository.save(foodArticleOrder); 60 | return convertToDto(foodArticleOrder); 61 | } 62 | 63 | 64 | public void deleteFoodArticleOrder(OrderFoodArticleDto orderFoodArticleDto) { 65 | Optional foodArticleOrder = foodArticleOrderRepository.findFoodArticleOrderByFoodArticleIdAndOrderId( 66 | orderFoodArticleDto.getFoodArticleId(), 67 | orderFoodArticleDto.getOrderId()); 68 | 69 | foodArticleOrder.ifPresent(articleOrder -> foodArticleOrderRepository.delete(articleOrder)); 70 | } 71 | 72 | public OrderFoodArticleDto updateFoodArticleOrder(OrderFoodArticleDto orderFoodArticleDto) { 73 | Optional foodArticleOrder = foodArticleOrderRepository.findFoodArticleOrderByFoodArticleIdAndOrderId( 74 | orderFoodArticleDto.getFoodArticleId(), 75 | orderFoodArticleDto.getOrderId()); 76 | 77 | if (foodArticleOrder.isEmpty()) return createFoodArticleOrder(orderFoodArticleDto); 78 | 79 | foodArticleOrder.get().setQuantity(orderFoodArticleDto.getQuantity()); 80 | foodArticleOrderRepository.save(foodArticleOrder.get()); 81 | return convertToDto(foodArticleOrder.get()); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/FoodArticleService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.dto.FoodArticleDto; 4 | import com.pyropy.usereats.dto.RestaurantDto; 5 | import com.pyropy.usereats.model.FoodArticle; 6 | import com.pyropy.usereats.model.Restaurant; 7 | import com.pyropy.usereats.repository.FoodArticleRepository; 8 | import org.modelmapper.ModelMapper; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.expression.ExpressionException; 11 | import org.springframework.stereotype.Service; 12 | 13 | import javax.persistence.EntityNotFoundException; 14 | import java.util.List; 15 | import java.util.stream.Collectors; 16 | import java.util.stream.StreamSupport; 17 | 18 | @Service 19 | public class FoodArticleService { 20 | 21 | @Autowired 22 | private FoodArticleRepository foodArticleRepository; 23 | 24 | @Autowired 25 | private RestaurantService restaurantService; 26 | 27 | @Autowired 28 | private ModelMapper modelMapper; 29 | 30 | 31 | /* 32 | * Converts FoodArticle Entity to FoodArticleDto class. 33 | * */ 34 | public FoodArticleDto convertToDto(FoodArticle foodArticle) { 35 | return modelMapper.map(foodArticle, FoodArticleDto.class); 36 | } 37 | 38 | /* 39 | * Converts FoodArticleDto to FoodArticle entity. 40 | * */ 41 | public FoodArticle convertToEntity(FoodArticleDto foodArticleDto) { 42 | return modelMapper.map(foodArticleDto, FoodArticle.class); 43 | } 44 | 45 | public List convertIterableToListDto(Iterable foodArticles) { 46 | return StreamSupport 47 | .stream(foodArticles.spliterator(), false) 48 | .map(this::convertToDto) 49 | .collect(Collectors.toList()); 50 | } 51 | 52 | public FoodArticleDto findById(Long id) { 53 | return foodArticleRepository.findById(id).map(this::convertToDto) 54 | .orElseThrow(() -> new EntityNotFoundException("Food article not found.")); 55 | } 56 | 57 | public List findAll() { 58 | return convertIterableToListDto(foodArticleRepository.findAll()); 59 | } 60 | 61 | public void deleteFoodArticle(FoodArticleDto foodArticleDto, String username) { 62 | foodArticleRepository 63 | .findFoodArticleByIdAndRestaurantOwnerUsername(foodArticleDto.getId(), username) 64 | .map(foodArticle -> { 65 | foodArticleRepository.delete(foodArticle); 66 | return null; 67 | }); 68 | } 69 | 70 | public FoodArticleDto updateFoodArticle(FoodArticleDto foodArticleDto, String username) { 71 | return foodArticleRepository 72 | .findFoodArticleByIdAndRestaurantOwnerUsername(foodArticleDto.getId(), username) 73 | .map(foodArticle -> { 74 | foodArticle.setName(foodArticle.getName()); 75 | foodArticle.setPrice(foodArticleDto.getPrice()); 76 | foodArticle.setImageUrl(foodArticleDto.getImageUrl()); 77 | foodArticle.setDescription(foodArticleDto.getDescription()); 78 | return convertToDto(foodArticle); 79 | }) 80 | .orElseThrow(() -> new ExpressionException("Food article not found.")); 81 | } 82 | 83 | public List findFoodArticlesByRestaurantId(Long id) { 84 | return convertIterableToListDto(foodArticleRepository.findFoodArticleByRestaurantId(id)); 85 | } 86 | 87 | public FoodArticleDto createFoodArticle(FoodArticleDto foodArticleDto, String username) { 88 | RestaurantDto restaurantDto = restaurantService 89 | .findRestaurantByIdAndOwnerUsername(foodArticleDto.getRestaurantId(), username); 90 | Restaurant restaurant = restaurantService.convertToEntity(restaurantDto); 91 | FoodArticle foodArticle = convertToEntity(foodArticleDto); 92 | foodArticle.setRestaurant(restaurant); 93 | foodArticleRepository.save(foodArticle); 94 | return convertToDto(foodArticle); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/RestaurantService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.dto.RestaurantDto; 4 | import com.pyropy.usereats.model.Restaurant; 5 | import com.pyropy.usereats.model.User; 6 | import com.pyropy.usereats.repository.RestaurantRepository; 7 | import org.modelmapper.ModelMapper; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import javax.persistence.EntityNotFoundException; 12 | import java.util.List; 13 | import java.util.stream.Collectors; 14 | import java.util.stream.StreamSupport; 15 | 16 | @Service 17 | public class RestaurantService { 18 | 19 | @Autowired 20 | private RestaurantRepository restaurantRepository; 21 | 22 | @Autowired 23 | private ModelMapper modelMapper; 24 | 25 | @Autowired 26 | private UserService userService; 27 | 28 | /* 29 | * Converts Restaurant Entity to RestaurantDto class. 30 | * */ 31 | public RestaurantDto convertToDto(Restaurant restaurant) { 32 | return modelMapper.map(restaurant, RestaurantDto.class); 33 | } 34 | 35 | /* 36 | * Converts RestaurantDto entity to Restaurant class. 37 | * */ 38 | public Restaurant convertToEntity(RestaurantDto restaurantDto) { 39 | return modelMapper.map(restaurantDto, Restaurant.class); 40 | } 41 | 42 | 43 | public List convertIterableToListDto(Iterable restaurants) { 44 | return StreamSupport 45 | .stream(restaurants.spliterator(), false) 46 | .map(this::convertToDto) 47 | .collect(Collectors.toList()); 48 | } 49 | 50 | public List findAll() { 51 | return convertIterableToListDto(restaurantRepository.findAll()); 52 | } 53 | 54 | /* 55 | * Find restaurant by given name. 56 | */ 57 | public List findByNameLike(String name) { 58 | Iterable restaurants = restaurantRepository.findByNameLike(name); 59 | return convertIterableToListDto(restaurants); 60 | } 61 | 62 | /* 63 | * Find restaurants by given owner username. 64 | */ 65 | public List findByOwnerUsername(String username) { 66 | Iterable restaurants = restaurantRepository.findRestaurantByOwnerUsername(username); 67 | return convertIterableToListDto(restaurants); 68 | } 69 | 70 | public RestaurantDto createRestaurant(RestaurantDto restaurantInfo, String username) { 71 | Restaurant restaurant = convertToEntity(restaurantInfo); 72 | User user = userService.convertToEntity(userService.findByUsername(username)); 73 | restaurant.setOwner(user); 74 | restaurantRepository.save(restaurant); 75 | return convertToDto(restaurant); 76 | } 77 | 78 | public RestaurantDto updateRestaurant(RestaurantDto restaurantDto, String username) { 79 | return restaurantRepository.findRestaurantByIdAndOwnerUsername(restaurantDto.getId(), username) 80 | .map(restaurant -> { 81 | restaurant.setName(restaurant.getName()); 82 | restaurant.setDescription(restaurant.getDescription()); 83 | restaurantRepository.save(restaurant); 84 | return convertToDto(restaurant); 85 | }) 86 | .orElseThrow(() -> new EntityNotFoundException("Restaurant not found.")); 87 | } 88 | 89 | public void deleteRestaurant(RestaurantDto restaurantDto, String username) { 90 | restaurantRepository 91 | .findRestaurantByIdAndOwnerUsername(restaurantDto.getId(), username) 92 | .map(restaurant -> { 93 | restaurantRepository.delete(restaurant); 94 | return null; 95 | }); 96 | } 97 | 98 | public RestaurantDto findRestaurantByIdAndOwnerUsername(Long restaurantId, String username) { 99 | return restaurantRepository 100 | .findRestaurantByIdAndOwnerUsername(restaurantId, username) 101 | .map(this::convertToDto) 102 | .orElseThrow(() -> new EntityNotFoundException("Restaurant not found.")); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/SendGridEmailService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.config.SendGridConfig; 4 | import com.pyropy.usereats.dto.NotificationDto; 5 | import com.pyropy.usereats.model.FoodArticle; 6 | import com.pyropy.usereats.model.FoodArticleOrder; 7 | import com.pyropy.usereats.model.Order; 8 | import com.pyropy.usereats.model.User; 9 | import com.sendgrid.Method; 10 | import com.sendgrid.Request; 11 | import com.sendgrid.Response; 12 | import com.sendgrid.SendGrid; 13 | import com.sendgrid.helpers.mail.Mail; 14 | import com.sendgrid.helpers.mail.objects.Content; 15 | import com.sendgrid.helpers.mail.objects.Email; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | 19 | import java.io.IOException; 20 | import java.util.*; 21 | import java.util.stream.Collectors; 22 | 23 | @Service("sendGrid") // todo: Make notifications async 24 | public class SendGridEmailService implements UserNotificationService { 25 | 26 | @Autowired 27 | private SendGridConfig sendGridConfig; 28 | 29 | @Autowired 30 | private SendGrid sendGridClient; 31 | 32 | private Response sendEmail(String from, String to, String subject, Content content) { 33 | Mail mail = new Mail(new Email(from), subject, new Email(to), content); 34 | mail.setReplyTo(new Email(from)); 35 | Request request = new Request(); 36 | Response response = null; 37 | try { 38 | request.setMethod(Method.POST); 39 | request.setEndpoint("mail/send"); 40 | request.setBody(mail.build()); 41 | this.sendGridClient.api(request); 42 | } catch (IOException ex) { 43 | System.out.println(ex.getMessage()); 44 | } 45 | return response; 46 | } 47 | 48 | @Override 49 | public void notifyUser(User from, User to, NotificationDto notificationDto) { 50 | Content content = new Content("text/html", notificationDto.getContent()); 51 | sendEmail(from.getEmail(), to.getEmail(), notificationDto.getSubject(), content); 52 | } 53 | 54 | /* Notifies restaurant owners upon user order confirmation */ 55 | public void notifyRestaurantOwnersConfirmedOrder(Order order) { 56 | User notificationUser = getDefaultNotificationUser(); 57 | createOwnerOrdersMap(order).forEach((owner, foodArticles) -> { 58 | NotificationDto notificationDto = generateRestaurantOwnerOrderEmail(foodArticles, order.getDeliveryAddress()); 59 | notifyUser(notificationUser, owner, notificationDto); 60 | }); 61 | } 62 | 63 | private User getDefaultNotificationUser() { 64 | // todo: replace this with more elegant solution 65 | User notificationUser = new User(); 66 | notificationUser.setEmail(sendGridConfig.getDefaultSenderEmail()); // todo: load email from config 67 | return notificationUser; 68 | } 69 | 70 | private NotificationDto generateRestaurantOwnerOrderEmail(List foodArticles, String deliveryAdders) { 71 | // todo: load html template 72 | String notificationContent = foodArticles 73 | .stream() 74 | .map(foodArticleOrder -> new StringBuilder() 75 | .append(foodArticleOrder.getFoodArticle().getName()) 76 | .append(" - ").append(foodArticleOrder.getQuantity().toString()) 77 | .toString()) 78 | .collect(Collectors.joining()); 79 | return new NotificationDto("New Order", String.format("%s\n%s", notificationContent, deliveryAdders)); 80 | } 81 | 82 | /* Creates HashMap with list of FoodArticleOrders for each restaurant owner */ 83 | private HashMap> createOwnerOrdersMap(Order order) { 84 | HashMap> ownerOrders = new HashMap<>(); 85 | 86 | order.getFoodArticles() 87 | .forEach(foodArticleOrder -> { 88 | FoodArticle foodArticle = foodArticleOrder.getFoodArticle(); 89 | User owner = foodArticle.getRestaurant().getOwner(); 90 | if (ownerOrders.containsKey(owner)) 91 | ownerOrders.get(owner).add(foodArticleOrder); 92 | else ownerOrders.put(owner, Collections.singletonList(foodArticleOrder)); 93 | }); 94 | 95 | return ownerOrders; 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/security/SecurityConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.security; 2 | 3 | import com.pyropy.usereats.config.JwtConfig; 4 | import com.pyropy.usereats.jwt.JwtTokenVerifier; 5 | import com.pyropy.usereats.jwt.JwtUsernameAndPasswordAuthFilter; 6 | import com.pyropy.usereats.service.UserSecurityDetailsService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.http.HttpMethod; 11 | import org.springframework.security.authentication.dao.DaoAuthenticationProvider; 12 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 13 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 14 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 15 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 16 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 17 | import org.springframework.security.config.http.SessionCreationPolicy; 18 | import org.springframework.security.crypto.password.PasswordEncoder; 19 | 20 | import javax.crypto.SecretKey; 21 | 22 | 23 | @Configuration 24 | @EnableWebSecurity 25 | @EnableGlobalMethodSecurity(prePostEnabled = true) 26 | public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 27 | 28 | private final UserSecurityDetailsService userDetailsService; 29 | private final PasswordEncoder passwordEncoder; 30 | private final JwtConfig jwtConfig; 31 | private final SecretKey secretKey; 32 | 33 | @Autowired 34 | public SecurityConfiguration(UserSecurityDetailsService userDetailsService, 35 | PasswordEncoder passwordEncoder, 36 | JwtConfig jwtConfig, 37 | SecretKey secretKey) { 38 | this.userDetailsService = userDetailsService; 39 | this.passwordEncoder = passwordEncoder; 40 | this.jwtConfig = jwtConfig; 41 | this.secretKey = secretKey; 42 | } 43 | 44 | @Override 45 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { 46 | auth.authenticationProvider(authenticationProvider()); 47 | } 48 | 49 | @Override 50 | protected void configure(HttpSecurity http) throws Exception { 51 | http 52 | .csrf().disable() 53 | .sessionManagement() 54 | .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 55 | .and() 56 | .addFilter(new JwtUsernameAndPasswordAuthFilter(authenticationManager(), jwtConfig, secretKey)) 57 | .addFilterAfter(new JwtTokenVerifier(secretKey, jwtConfig), JwtUsernameAndPasswordAuthFilter.class) 58 | .authorizeRequests() 59 | .antMatchers(HttpMethod.POST, "/login").permitAll() 60 | .antMatchers(HttpMethod.GET, "/api/v1/food").permitAll() // allow anonymous users to list of food articles 61 | .antMatchers(HttpMethod.GET, "/api/v1/food").permitAll() // allow anonymous users to list of food articles 62 | .antMatchers(HttpMethod.POST, "/api/v1/users/**").permitAll() // allow users to register 63 | .and() 64 | .authorizeRequests() 65 | .antMatchers(HttpMethod.GET, "/api/v1/users/**").hasAnyRole("USER", "RESTAURANT_ADMIN") // allow users to register 66 | .antMatchers(HttpMethod.POST, "/api/v1/food").hasRole("RESTAURANT_ADMIN") 67 | .antMatchers(HttpMethod.POST, "/api/v1/restaurants/**").hasRole("RESTAURANT_ADMIN") 68 | .antMatchers(HttpMethod.DELETE, "/api/v1/restaurants/**").hasRole("RESTAURANT_ADMIN") 69 | .antMatchers(HttpMethod.PUT, "/api/v1/restaurants/**").hasRole("RESTAURANT_ADMIN") 70 | .antMatchers(HttpMethod.GET, "/api/v1/order/**").hasAnyRole("USER", "RESTAURANT_ADMIN") 71 | .antMatchers(HttpMethod.POST, "/api/v1/order/**").hasAnyRole("USER", "RESTAURANT_ADMIN") 72 | .antMatchers(HttpMethod.DELETE, "/api/v1/order/**").hasAnyRole("USER", "RESTAURANT_ADMIN") 73 | .antMatchers(HttpMethod.PUT, "/api/v1/order/**").hasAnyRole("USER", "RESTAURANT_ADMIN") 74 | .anyRequest() 75 | .authenticated(); 76 | } 77 | 78 | @Bean 79 | public DaoAuthenticationProvider authenticationProvider() { 80 | DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); 81 | provider.setPasswordEncoder(passwordEncoder); 82 | provider.setUserDetailsService(userDetailsService); 83 | return provider; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.4.RELEASE 9 | 10 | 11 | com.pyropy 12 | usereats 13 | 0.0.1-SNAPSHOT 14 | usereats 15 | User Eats 16 | 17 | 18 | 11 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-security 29 | 30 | 31 | 32 | org.springframework.data 33 | spring-data-rest-webmvc 34 | 3.2.6.RELEASE 35 | 36 | 37 | 38 | com.google.guava 39 | guava 40 | 29.0-jre 41 | 42 | 43 | 44 | io.jsonwebtoken 45 | jjwt-api 46 | 0.11.0 47 | 48 | 49 | io.jsonwebtoken 50 | jjwt-impl 51 | 0.11.0 52 | runtime 53 | 54 | 55 | io.jsonwebtoken 56 | jjwt-jackson 57 | 0.11.0 58 | runtime 59 | 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-starter-web 64 | 65 | 66 | 67 | mysql 68 | mysql-connector-java 69 | runtime 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-starter-test 75 | test 76 | 77 | 78 | org.junit.vintage 79 | junit-vintage-engine 80 | 81 | 82 | 83 | 84 | org.springframework.security 85 | spring-security-test 86 | test 87 | 88 | 89 | org.jetbrains 90 | annotations 91 | RELEASE 92 | compile 93 | 94 | 95 | org.springframework.boot 96 | spring-boot-starter-mail 97 | 98 | 99 | com.sendgrid 100 | sendgrid-java 101 | 4.4.6 102 | 103 | 104 | org.modelmapper 105 | modelmapper 106 | 2.3.5 107 | 108 | 109 | 110 | org.modelmapper.extensions 111 | modelmapper-jackson 112 | 2.3.0 113 | 114 | 115 | 116 | org.projectlombok 117 | lombok 118 | 1.18.12 119 | provided 120 | 121 | 122 | org.assertj 123 | assertj-core 124 | 125 | 126 | 127 | 128 | 129 | 130 | org.springframework.boot 131 | spring-boot-maven-plugin 132 | 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import java.net.*; 18 | import java.io.*; 19 | import java.nio.channels.*; 20 | import java.util.Properties; 21 | 22 | public class MavenWrapperDownloader { 23 | 24 | private static final String WRAPPER_VERSION = "0.5.6"; 25 | /** 26 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 27 | */ 28 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 29 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 30 | 31 | /** 32 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 33 | * use instead of the default one. 34 | */ 35 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 36 | ".mvn/wrapper/maven-wrapper.properties"; 37 | 38 | /** 39 | * Path where the maven-wrapper.jar will be saved to. 40 | */ 41 | private static final String MAVEN_WRAPPER_JAR_PATH = 42 | ".mvn/wrapper/maven-wrapper.jar"; 43 | 44 | /** 45 | * Name of the property which should be used to override the default download url for the wrapper. 46 | */ 47 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 48 | 49 | public static void main(String args[]) { 50 | System.out.println("- Downloader started"); 51 | File baseDirectory = new File(args[0]); 52 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 53 | 54 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 55 | // wrapperUrl parameter. 56 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 57 | String url = DEFAULT_DOWNLOAD_URL; 58 | if (mavenWrapperPropertyFile.exists()) { 59 | FileInputStream mavenWrapperPropertyFileInputStream = null; 60 | try { 61 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 62 | Properties mavenWrapperProperties = new Properties(); 63 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 64 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 65 | } catch (IOException e) { 66 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 67 | } finally { 68 | try { 69 | if (mavenWrapperPropertyFileInputStream != null) { 70 | mavenWrapperPropertyFileInputStream.close(); 71 | } 72 | } catch (IOException e) { 73 | // Ignore ... 74 | } 75 | } 76 | } 77 | System.out.println("- Downloading from: " + url); 78 | 79 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 80 | if (!outputFile.getParentFile().exists()) { 81 | if (!outputFile.getParentFile().mkdirs()) { 82 | System.out.println( 83 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 84 | } 85 | } 86 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 87 | try { 88 | downloadFileFromURL(url, outputFile); 89 | System.out.println("Done"); 90 | System.exit(0); 91 | } catch (Throwable e) { 92 | System.out.println("- Error downloading"); 93 | e.printStackTrace(); 94 | System.exit(1); 95 | } 96 | } 97 | 98 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 99 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 100 | String username = System.getenv("MVNW_USERNAME"); 101 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 102 | Authenticator.setDefault(new Authenticator() { 103 | @Override 104 | protected PasswordAuthentication getPasswordAuthentication() { 105 | return new PasswordAuthentication(username, password); 106 | } 107 | }); 108 | } 109 | URL website = new URL(urlString); 110 | ReadableByteChannel rbc; 111 | rbc = Channels.newChannel(website.openStream()); 112 | FileOutputStream fos = new FileOutputStream(destination); 113 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 114 | fos.close(); 115 | rbc.close(); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/com/pyropy/usereats/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.pyropy.usereats.service; 2 | 3 | import com.pyropy.usereats.dto.OrderFoodArticleDto; 4 | import com.pyropy.usereats.dto.OrderDto; 5 | import com.pyropy.usereats.dto.UserDto; 6 | import com.pyropy.usereats.model.*; 7 | import com.pyropy.usereats.repository.OrderRepository; 8 | import org.modelmapper.ModelMapper; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Service; 11 | 12 | import javax.persistence.EntityNotFoundException; 13 | import java.util.List; 14 | import java.util.stream.Collectors; 15 | import java.util.stream.StreamSupport; 16 | 17 | @Service 18 | public class OrderService { 19 | 20 | @Autowired 21 | private OrderRepository orderRepository; 22 | 23 | @Autowired 24 | private ModelMapper modelMapper; 25 | 26 | @Autowired 27 | private UserService userService; 28 | 29 | @Autowired 30 | private FoodArticleOrderService foodArticleOrderService; 31 | 32 | @Autowired 33 | private SendGridEmailService sendGridEmailService; 34 | 35 | /* 36 | * Converts Order Entity to OrderDto class. 37 | * */ 38 | public OrderDto convertToDto(Order order) { 39 | return modelMapper.map(order, OrderDto.class); 40 | } 41 | 42 | /* 43 | * Converts OrderDto entity to Order class. 44 | * */ 45 | public Order convertToEntity(OrderDto orderDto) { 46 | return modelMapper.map(orderDto, Order.class); 47 | } 48 | 49 | public List convertIterableToListDto(Iterable orders) { 50 | return StreamSupport 51 | .stream(orders.spliterator(), false) 52 | .map(this::convertToDto) 53 | .collect(Collectors.toList()); 54 | } 55 | 56 | public Order findEntityById(Long id) { 57 | return this.orderRepository.findById(id) 58 | .orElseThrow(() -> new EntityNotFoundException("Order not found.")); 59 | } 60 | 61 | /* Creates new order for user if one not already opened */ 62 | public OrderDto createOrder(OrderFoodArticleDto orderFoodArticleDto, String username) { 63 | User user = userService.convertToEntity(userService.findByUsername(username)); 64 | Order order = new Order(); 65 | order.setUser(user); 66 | order.setOrderStatus(OrderStatus.OPEN); 67 | orderRepository.save(order); 68 | return updateOrderWithFoodArticle(convertToDto(order), orderFoodArticleDto, false); 69 | } 70 | 71 | public List findUserOrders(String username) { 72 | return convertIterableToListDto(orderRepository.findOrderByUserUsername(username)); 73 | } 74 | 75 | public OrderDto findOrderByIdAndUsername(Long id, String username) { 76 | return orderRepository.findOrderByIdAndUserUsername(id, username) 77 | .map(this::convertToDto) 78 | .orElseThrow(() -> new EntityNotFoundException("Order not found.")); 79 | } 80 | 81 | public OrderDto updateOrderArticles(OrderFoodArticleDto orderFoodArticleDto, String username) { 82 | OrderDto orderDto = findOrderByIdAndUsername(orderFoodArticleDto.getOrderId(), username); 83 | return updateOrderWithFoodArticle(orderDto, orderFoodArticleDto, false); 84 | } 85 | 86 | public OrderDto deleteOrderArticle(OrderFoodArticleDto orderFoodArticleDto, String username) { 87 | OrderDto orderDto = findOrderByIdAndUsername(orderFoodArticleDto.getOrderId(), username); 88 | return updateOrderWithFoodArticle(orderDto, orderFoodArticleDto, true); 89 | } 90 | 91 | private OrderDto updateOrderWithFoodArticle(OrderDto orderDto, OrderFoodArticleDto orderFoodArticleDto, boolean delete) { 92 | Order order = findEntityById(orderDto.getId()); 93 | orderFoodArticleDto.setOrderId(order.getId()); // double check 94 | if (delete) foodArticleOrderService.deleteFoodArticleOrder(orderFoodArticleDto); 95 | else foodArticleOrderService.updateFoodArticleOrder(orderFoodArticleDto); 96 | order.setSubtotal(calculateSubtotal(order)); 97 | order.setTotal(calculateTotal(order)); 98 | orderRepository.save(order); 99 | return convertToDto(order); 100 | } 101 | 102 | public OrderDto updateOrderStatus(OrderDto orderDto, OrderStatus orderStatus, String username) { 103 | UserDto userDto = userService.findByUsername(username); 104 | return orderRepository.findOrderByIdAndUserUsername(orderDto.getId(), username) 105 | .map(order -> { 106 | if (order.getOrderStatus() == OrderStatus.CONFIRMED) return convertToDto(order); 107 | if (orderStatus == OrderStatus.CONFIRMED) 108 | sendGridEmailService.notifyRestaurantOwnersConfirmedOrder(order); 109 | 110 | String deliveryAddress = orderDto.getDeliveryAddress().isEmpty() ? userDto.getAddress() : orderDto.getDeliveryAddress(); 111 | order.setDeliveryAddress(deliveryAddress); 112 | order.setOrderStatus(orderStatus); 113 | orderRepository.save(order); 114 | return convertToDto(order); 115 | }).orElseThrow(() -> new EntityNotFoundException("Order not found")); 116 | } 117 | 118 | 119 | private Float calculateSubtotal(Order order) { 120 | return order.getFoodArticles() 121 | .stream() 122 | .map(article -> article.getQuantity() * article.getFoodArticle().getPrice()) 123 | .reduce(0f, Float::sum); 124 | } 125 | 126 | private Float calculateTotal(Order order) { 127 | return calculateSubtotal(order); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 124 | 125 | FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 162 | if ERRORLEVEL 1 goto error 163 | goto end 164 | 165 | :error 166 | set ERROR_CODE=1 167 | 168 | :end 169 | @endlocal & set ERROR_CODE=%ERROR_CODE% 170 | 171 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 172 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 173 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 174 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 175 | :skipRcPost 176 | 177 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 178 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 179 | 180 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 181 | 182 | exit /B %ERROR_CODE% 183 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | fi 118 | 119 | if [ -z "$JAVA_HOME" ]; then 120 | javaExecutable="`which javac`" 121 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 122 | # readlink(1) is not available as standard on Solaris 10. 123 | readLink=`which readlink` 124 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 125 | if $darwin ; then 126 | javaHome="`dirname \"$javaExecutable\"`" 127 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 128 | else 129 | javaExecutable="`readlink -f \"$javaExecutable\"`" 130 | fi 131 | javaHome="`dirname \"$javaExecutable\"`" 132 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 133 | JAVA_HOME="$javaHome" 134 | export JAVA_HOME 135 | fi 136 | fi 137 | fi 138 | 139 | if [ -z "$JAVACMD" ] ; then 140 | if [ -n "$JAVA_HOME" ] ; then 141 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 142 | # IBM's JDK on AIX uses strange locations for the executables 143 | JAVACMD="$JAVA_HOME/jre/sh/java" 144 | else 145 | JAVACMD="$JAVA_HOME/bin/java" 146 | fi 147 | else 148 | JAVACMD="`which java`" 149 | fi 150 | fi 151 | 152 | if [ ! -x "$JAVACMD" ] ; then 153 | echo "Error: JAVA_HOME is not defined correctly." >&2 154 | echo " We cannot execute $JAVACMD" >&2 155 | exit 1 156 | fi 157 | 158 | if [ -z "$JAVA_HOME" ] ; then 159 | echo "Warning: JAVA_HOME environment variable is not set." 160 | fi 161 | 162 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 163 | 164 | # traverses directory structure from process work directory to filesystem root 165 | # first directory with .mvn subdirectory is considered project base directory 166 | find_maven_basedir() { 167 | 168 | if [ -z "$1" ] 169 | then 170 | echo "Path not specified to find_maven_basedir" 171 | return 1 172 | fi 173 | 174 | basedir="$1" 175 | wdir="$1" 176 | while [ "$wdir" != '/' ] ; do 177 | if [ -d "$wdir"/.mvn ] ; then 178 | basedir=$wdir 179 | break 180 | fi 181 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 182 | if [ -d "${wdir}" ]; then 183 | wdir=`cd "$wdir/.."; pwd` 184 | fi 185 | # end of workaround 186 | done 187 | echo "${basedir}" 188 | } 189 | 190 | # concatenates all lines of a file 191 | concat_lines() { 192 | if [ -f "$1" ]; then 193 | echo "$(tr -s '\n' ' ' < "$1")" 194 | fi 195 | } 196 | 197 | BASE_DIR=`find_maven_basedir "$(pwd)"` 198 | if [ -z "$BASE_DIR" ]; then 199 | exit 1; 200 | fi 201 | 202 | ########################################################################################## 203 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 204 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 205 | ########################################################################################## 206 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 207 | if [ "$MVNW_VERBOSE" = true ]; then 208 | echo "Found .mvn/wrapper/maven-wrapper.jar" 209 | fi 210 | else 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 213 | fi 214 | if [ -n "$MVNW_REPOURL" ]; then 215 | jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 216 | else 217 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 218 | fi 219 | while IFS="=" read key value; do 220 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 221 | esac 222 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 223 | if [ "$MVNW_VERBOSE" = true ]; then 224 | echo "Downloading from: $jarUrl" 225 | fi 226 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 227 | if $cygwin; then 228 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 229 | fi 230 | 231 | if command -v wget > /dev/null; then 232 | if [ "$MVNW_VERBOSE" = true ]; then 233 | echo "Found wget ... using wget" 234 | fi 235 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 236 | wget "$jarUrl" -O "$wrapperJarPath" 237 | else 238 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" 239 | fi 240 | elif command -v curl > /dev/null; then 241 | if [ "$MVNW_VERBOSE" = true ]; then 242 | echo "Found curl ... using curl" 243 | fi 244 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 245 | curl -o "$wrapperJarPath" "$jarUrl" -f 246 | else 247 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 248 | fi 249 | 250 | else 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo "Falling back to using Java to download" 253 | fi 254 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 255 | # For Cygwin, switch paths to Windows format before running javac 256 | if $cygwin; then 257 | javaClass=`cygpath --path --windows "$javaClass"` 258 | fi 259 | if [ -e "$javaClass" ]; then 260 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 261 | if [ "$MVNW_VERBOSE" = true ]; then 262 | echo " - Compiling MavenWrapperDownloader.java ..." 263 | fi 264 | # Compiling the Java class 265 | ("$JAVA_HOME/bin/javac" "$javaClass") 266 | fi 267 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 268 | # Running the downloader 269 | if [ "$MVNW_VERBOSE" = true ]; then 270 | echo " - Running MavenWrapperDownloader.java ..." 271 | fi 272 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 273 | fi 274 | fi 275 | fi 276 | fi 277 | ########################################################################################## 278 | # End of extension 279 | ########################################################################################## 280 | 281 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 282 | if [ "$MVNW_VERBOSE" = true ]; then 283 | echo $MAVEN_PROJECTBASEDIR 284 | fi 285 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 286 | 287 | # For Cygwin, switch paths to Windows format before running java 288 | if $cygwin; then 289 | [ -n "$M2_HOME" ] && 290 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 291 | [ -n "$JAVA_HOME" ] && 292 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 293 | [ -n "$CLASSPATH" ] && 294 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 295 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 296 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 297 | fi 298 | 299 | # Provide a "standardized" way to retrieve the CLI args that will 300 | # work with both Windows and non-Windows executions. 301 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 302 | export MAVEN_CMD_LINE_ARGS 303 | 304 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 305 | 306 | exec "$JAVACMD" \ 307 | $MAVEN_OPTS \ 308 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 309 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 310 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 311 | --------------------------------------------------------------------------------