create a Food delivery Application by using frontend as reactJS and backend as…
- User
create a Food delivery Application by using frontend as reactJS and backend as springboot and database as mysql
Assistant
create a Food delivery Application by using frontend as reactJS and backend as springboot and database as mysql
This document outlines the architecture and implementation details for a full-stack food delivery application using:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ ReactJS │ ←→ │ Spring Boot │ ←→ │ MySQL │
│ Frontend │ │ Backend │ │ Database │
└─────────────┘ └─────────────┘ └─────────────┘
-- Users table
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
phone VARCHAR(20),
address TEXT,
role ENUM('CUSTOMER', 'RESTAURANT_OWNER', 'ADMIN', 'DELIVERY_PERSON') NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Restaurants table
CREATE TABLE restaurants (
restaurant_id INT AUTO_INCREMENT PRIMARY KEY,
owner_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
description TEXT,
cuisine_type VARCHAR(50),
address TEXT NOT NULL,
contact_number VARCHAR(20),
opening_hours VARCHAR(100),
image_url VARCHAR(255),
rating DECIMAL(3,2) DEFAULT 0,
FOREIGN KEY (owner_id) REFERENCES users(user_id)
);
-- Menu Items table
CREATE TABLE menu_items (
item_id INT AUTO_INCREMENT PRIMARY KEY,
restaurant_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
description TEXT,
price DECIMAL(10,2) NOT NULL,
category VARCHAR(50),
image_url VARCHAR(255),
is_available BOOLEAN DEFAULT TRUE,
FOREIGN KEY (restaurant_id) REFERENCES restaurants(restaurant_id)
);
-- Orders table
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
restaurant_id INT NOT NULL,
delivery_person_id INT,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
delivery_address TEXT NOT NULL,
status ENUM('PENDING', 'ACCEPTED', 'COOKING', 'READY', 'ON_THE_WAY', 'DELIVERED', 'CANCELLED') NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
payment_method VARCHAR(50),
payment_status ENUM('PENDING', 'COMPLETED', 'FAILED', 'REFUNDED') NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (restaurant_id) REFERENCES restaurants(restaurant_id),
FOREIGN KEY (delivery_person_id) REFERENCES users(user_id)
);
-- Order Items table
CREATE TABLE order_items (
order_item_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
item_id INT NOT NULL,
quantity INT NOT NULL,
price_at_order DECIMAL(10,2) NOT NULL,
special_instructions TEXT,
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (item_id) REFERENCES menu_items(item_id)
);
-- Reviews table
CREATE TABLE reviews (
review_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
restaurant_id INT NOT NULL,
order_id INT NOT NULL,
rating INT NOT NULL CHECK (rating BETWEEN 1 AND 5),
comment TEXT,
review_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (restaurant_id) REFERENCES restaurants(restaurant_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
src/main/java/com/fooddelivery/
├── config/ # Configuration classes
├── controller/ # REST controllers
├── dto/ # Data Transfer Objects
├── exception/ # Custom exceptions
├── model/ # Entity classes
├── repository/ # JPA repositories
├── security/ # Security configuration
├── service/ # Business logic
└── FoodDeliveryApplication.java
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public ResponseEntity<UserDTO> registerUser(@Valid @RequestBody UserRegistrationDTO registrationDTO) {
UserDTO createdUser = userService.registerUser(registrationDTO);
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}
@PostMapping("/login")
public ResponseEntity<JwtResponse> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {
String jwt = userService.authenticateUser(loginRequest);
return ResponseEntity.ok(new JwtResponse(jwt));
}
@GetMapping("/profile")
@PreAuthorize("hasRole('CUSTOMER') or hasRole('RESTAURANT_OWNER') or hasRole('DELIVERY_PERSON')")
public ResponseEntity<UserDTO> getUserProfile(Principal principal) {
UserDTO userProfile = userService.getUserProfile(principal.getName());
return ResponseEntity.ok(userProfile);
}
@PutMapping("/profile")
@PreAuthorize("hasRole('CUSTOMER') or hasRole('RESTAURANT_OWNER') or hasRole('DELIVERY_PERSON')")
public ResponseEntity<UserDTO> updateUserProfile(
Principal principal,
@Valid @RequestBody UserUpdateDTO updateDTO) {
UserDTO updatedUser = userService.updateUserProfile(principal.getName(), updateDTO);
return ResponseEntity.ok(updatedUser);
}
}
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final AuthenticationManager authenticationManager;
private final JwtUtils jwtUtils;
private final ModelMapper modelMapper;
@Override
public UserDTO registerUser(UserRegistrationDTO registrationDTO) {
if (userRepository.existsByUsername(registrationDTO.getUsername())) {
throw new UsernameAlreadyExistsException("Username is already taken!");
}
if (userRepository.existsByEmail(registrationDTO.getEmail())) {
throw new EmailAlreadyExistsException("Email is already in use!");
}
User user = new User();
user.setUsername(registrationDTO.getUsername());
user.setEmail(registrationDTO.getEmail());
user.setPassword(passwordEncoder.encode(registrationDTO.getPassword()));
user.setPhone(registrationDTO.getPhone());
user.setAddress(registrationDTO.getAddress());
user.setRole(registrationDTO.getRole());
User savedUser = userRepository.save(user);
return modelMapper.map(savedUser, UserDTO.class);
}
@Override
public String authenticateUser(LoginRequest loginRequest) {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
loginRequest.getUsername(),
loginRequest.getPassword()
)
);
SecurityContextHolder.getContext().setAuthentication(authentication);
return jwtUtils.generateJwtToken(authentication);
}
@Override
public UserDTO getUserProfile(String username) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UserNotFoundException("User not found with username: " + username