Skip to content

Commit d83632d

Browse files
author
Mohaned Atef
committed
Added "controllers" and repository classes to communicate with database to maintain code cleanliness
1 parent 916d683 commit d83632d

File tree

9 files changed

+130
-9
lines changed

9 files changed

+130
-9
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.iluwatar.monolithic.controller;
2+
3+
import com.iluwatar.monolithic.model.User;
4+
import com.iluwatar.monolithic.model.Orders;
5+
import com.iluwatar.monolithic.model.Products;
6+
import com.iluwatar.monolithic.repository.OrderRepo;
7+
import com.iluwatar.monolithic.repository.ProductRepo;
8+
import com.iluwatar.monolithic.repository.UserRepo;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
@Service
13+
public class OrderCon {
14+
@Autowired
15+
private OrderRepo orderRepository;
16+
17+
@Autowired
18+
private UserRepo userRepository;
19+
20+
@Autowired
21+
private ProductRepo productRepository;
22+
23+
public Orders placeOrder(Long userId, Long productId, Integer quantity) {
24+
User user = userRepository.findById(userId).orElse(null);
25+
if (user == null) {
26+
throw new RuntimeException("User not found");
27+
}
28+
29+
Products product = productRepository.findById(productId).orElse(null);
30+
if (product == null) {
31+
throw new RuntimeException("Product not found");
32+
}
33+
34+
if (product.getStock() < quantity) {
35+
throw new RuntimeException("Not enough stock");
36+
}
37+
38+
product.setStock(product.getStock() - quantity);
39+
productRepository.save(product);
40+
41+
Orders order = new Orders(null, user, product, quantity, product.getPrice() * quantity);
42+
return orderRepository.save(order);
43+
}
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.monolithic.controller;
2+
3+
import com.iluwatar.monolithic.model.Products;
4+
import com.iluwatar.monolithic.repository.ProductRepo;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
@Service
11+
public class ProductCon {
12+
@Autowired
13+
private ProductRepo productRepository;
14+
15+
public Products addProduct(Products product) {
16+
return productRepository.save(product);
17+
}
18+
19+
public List<Products> getAllProducts() {
20+
return productRepository.findAll();
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.iluwatar.monolithic.controller;
2+
3+
import com.iluwatar.monolithic.model.User;
4+
import com.iluwatar.monolithic.repository.UserRepo;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class UserCon {
10+
@Autowired
11+
private UserRepo userRepository;
12+
13+
public User registerUser(User user) {
14+
return userRepository.save(user);
15+
}
16+
}

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/model/Orders.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66
import jakarta.persistence.Id;
77
import jakarta.persistence.ManyToOne;
88
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
import lombok.AllArgsConstructor;
11+
912

1013
/**
1114
* Represents a Database in which Orders are stored.
1215
*/
1316
@Entity
1417
@Data
18+
@NoArgsConstructor
19+
@AllArgsConstructor
1520
public class Orders {
1621
@Id
17-
@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto-increment ID
22+
@GeneratedValue(strategy = GenerationType.IDENTITY)
1823
private Long id;
1924

20-
@ManyToOne // Relationship with User
25+
@ManyToOne
2126
private User user;
2227

23-
@ManyToOne // Relationship with Product
28+
@ManyToOne
2429
private Products product;
2530

2631
private Integer quantity;

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/model/Products.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
import jakarta.persistence.GeneratedValue;
55
import jakarta.persistence.GenerationType;
66
import jakarta.persistence.Id;
7+
78
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
import lombok.AllArgsConstructor;
811

912
/**
10-
* Represents a databse of products .
13+
* Represents a database of products.
1114
*/
1215
@Entity
1316
@Data
17+
@NoArgsConstructor
18+
@AllArgsConstructor
1419
public class Products {
1520
@Id
16-
@GeneratedValue(strategy = GenerationType.IDENTITY) // Generated Auto-increment ID
21+
@GeneratedValue(strategy = GenerationType.IDENTITY)
1722
private Long id;
1823

1924
private String name;
@@ -24,3 +29,4 @@ public class Products {
2429

2530
private Integer stock;
2631
}
32+

Monolithic-Ecommerce/src/main/java/com/iluwatar/monolithic/model/User.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@
55
import jakarta.persistence.GeneratedValue;
66
import jakarta.persistence.GenerationType;
77
import jakarta.persistence.Id;
8+
89
import lombok.Data;
10+
import lombok.NoArgsConstructor;
11+
import lombok.AllArgsConstructor;
912

1013
/**
11-
* Represents a User entity for the database to interact with the rest of the project.
14+
* Represents a Product entity for the database.
1215
*/
16+
1317
@Entity
1418
@Data
19+
@NoArgsConstructor
20+
@AllArgsConstructor
1521
public class User {
1622
@Id
17-
@GeneratedValue(strategy = GenerationType.IDENTITY) // Generated Auto-increment ID
23+
@GeneratedValue(strategy = GenerationType.IDENTITY)
1824
private Long id;
1925

2026
private String name;
2127

22-
@Column(unique = true) // Ensures unique emails
28+
@Column(unique = true)
2329
private String email;
2430

2531
private String password;
26-
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.monolithic.repository;
2+
3+
import com.iluwatar.monolithic.model.Orders;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface OrderRepo extends JpaRepository<Orders, Long> {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.monolithic.repository;
2+
3+
import com.iluwatar.monolithic.model.Products;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface ProductRepo extends JpaRepository<Products, Long> {
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar.monolithic.repository;
2+
3+
import com.iluwatar.monolithic.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface UserRepo extends JpaRepository<User, Long> {
7+
User findByEmail(String email);
8+
}

0 commit comments

Comments
 (0)