Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 67d20d0

Browse files
rename entity User to Customer
1 parent 6dcc24d commit 67d20d0

20 files changed

+237
-243
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ To view the API Documentation, run the code and navigate to http://localhost:808
2323
![category controller](https://github.com/vittorioexp/randombook-spring-rest-api/blob/main/img/API%20Documentation/category-controller.PNG)
2424
![feedback controller](https://github.com/vittorioexp/randombook-spring-rest-api/blob/main/img/API%20Documentation/feedback-controller.PNG)
2525
![reading controller](https://github.com/vittorioexp/randombook-spring-rest-api/blob/main/img/API%20Documentation/reading-controller.PNG)
26-
![user controller](https://github.com/vittorioexp/randombook-spring-rest-api/blob/main/img/API%20Documentation/user-controller.PNG)
26+
![customer controller](https://github.com/vittorioexp/randombook-spring-rest-api/blob/main/img/API%20Documentation/customer-controller.PNG)

‎mvnw.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ title %0
4343
@REM set %HOME% to equivalent of $HOME
4444
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
4545

46-
@REM Execute a user defined script before this one
46+
@REM Execute a customer defined script before this one
4747
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
4848
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
4949
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"

‎src/main/java/com/example/randombook/user/User.java renamed to ‎src/main/java/com/example/randombook/customer/Customer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.example.randombook.user;
1+
package com.example.randombook.customer;
22

3-
public class User {
3+
public class Customer {
44

55
private final int id;
66
private final String username;
77
private final String email;
88
private final String password;
99
private final String picture;
1010

11-
public User(int id, String username, String email, String password, String picture) {
11+
public Customer(int id, String username, String email, String password, String picture) {
1212
this.id = id;
1313
this.username = username;
1414
this.email = email;
@@ -38,7 +38,7 @@ public String getPicture() {
3838

3939
@Override
4040
public String toString() {
41-
return "User{" +
41+
return "Customer{" +
4242
"id=" + id +
4343
", username='" + username + '\'' +
4444
", email='" + email + '\'' +
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.randombook.customer;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.*;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
@RestController
10+
@RequestMapping("/customer")
11+
public class CustomerController {
12+
13+
private final JdbcCustomerDAO dao;
14+
15+
public CustomerController(JdbcCustomerDAO dao) {
16+
this.dao = dao;
17+
}
18+
19+
@GetMapping
20+
public List<Customer> findAll() {
21+
return dao.findAll();
22+
}
23+
24+
@GetMapping("/{id}")
25+
public Optional<Customer> findById(@PathVariable int id) {
26+
return dao.findById(id);
27+
}
28+
29+
@ResponseStatus(HttpStatus.CREATED)
30+
@PostMapping
31+
public Customer create(@RequestBody Customer customer) {
32+
return dao.create(customer);
33+
}
34+
35+
@PutMapping("/{id}")
36+
public Customer update(@RequestBody Customer customer, @PathVariable int id) {
37+
return dao.update(customer, id);
38+
}
39+
40+
@DeleteMapping("/{id}")
41+
public void delete(@PathVariable int id) {
42+
dao.delete(id);
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.randombook.customer;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
public interface CustomerDAO {
7+
List<Customer> findAll();
8+
Optional<Customer> findById(int id);
9+
Customer create(Customer customer);
10+
Customer update(Customer customer, int id);
11+
void delete(int id);
12+
long count();
13+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.example.randombook.customer;
2+
3+
import org.springframework.jdbc.core.JdbcTemplate;
4+
import org.springframework.jdbc.core.RowMapper;
5+
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.util.*;
9+
10+
@Repository
11+
public class JdbcCustomerDAO implements CustomerDAO {
12+
13+
private final List<Customer> customers;
14+
private final JdbcTemplate jdbcTemplate;
15+
private final SimpleJdbcInsert insertCustomer;
16+
17+
public JdbcCustomerDAO(JdbcTemplate jdbcTemplate) {
18+
this.customers = new ArrayList<>();
19+
this.jdbcTemplate = jdbcTemplate;
20+
insertCustomer = new SimpleJdbcInsert(jdbcTemplate)
21+
.withTableName("customer")
22+
.usingGeneratedKeyColumns("id_customer");
23+
}
24+
25+
RowMapper<Customer> rowMapper = (rs, rowNum) -> {
26+
return new Customer(
27+
rs.getInt("id_customer"),
28+
rs.getString("username"),
29+
rs.getString("email"),
30+
rs.getString("password"),
31+
rs.getString("picture")
32+
);
33+
};
34+
35+
@Override
36+
public List<Customer> findAll() {
37+
String sql = "SELECT * FROM \"customer\"";
38+
return jdbcTemplate.query(sql, rowMapper);
39+
}
40+
41+
@Override
42+
public Optional<Customer> findById(int id) {
43+
String sql = "SELECT * FROM \"customer\" where id_customer = ?";
44+
return Optional.of(jdbcTemplate.queryForObject(sql, rowMapper, id));
45+
}
46+
47+
@Override
48+
public Customer create(Customer customer) {
49+
Map<String,Object> parameters = new HashMap<>();
50+
parameters.put("username", customer.getUsername());
51+
parameters.put("email", customer.getEmail());
52+
parameters.put("password", customer.getPassword()); // Password should be encrypted
53+
parameters.put("picture", customer.getPicture());
54+
Number id_customer = insertCustomer.executeAndReturnKey(parameters);
55+
return new Customer(
56+
(Integer) id_customer,
57+
customer.getUsername(),
58+
customer.getEmail(),
59+
customer.getPassword(),
60+
customer.getPicture()
61+
);
62+
}
63+
64+
@Override
65+
public Customer update(Customer customer, int id) {
66+
String sql = "update \"customer\" set username = ?, " +
67+
"email = ?, password = ?, " +
68+
"picture = ? where id_customer = ?";
69+
Customer u = new Customer(
70+
id,
71+
customer.getUsername(),
72+
customer.getEmail(),
73+
customer.getPassword(),
74+
customer.getPicture()
75+
);
76+
jdbcTemplate.update(
77+
sql,
78+
u.getUsername(),
79+
u.getEmail(),
80+
u.getPassword(),
81+
u.getPicture(),
82+
u.getId()
83+
);
84+
return u;
85+
}
86+
87+
@Override
88+
public void delete(int id) {
89+
jdbcTemplate.update("delete from \"customer\" where id_customer = ?",id);
90+
}
91+
92+
@Override
93+
public long count() {
94+
return jdbcTemplate.queryForObject("select count(*) from \"customer\"", Long.class);
95+
}
96+
}

‎src/main/java/com/example/randombook/feedback/Feedback.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
public class Feedback {
66

77
private final int id;
8-
private final int user;
8+
private final int customer;
99
private final int book;
1010
private final int rate;
1111
private final String text;
1212
private final Date date;
1313

14-
public Feedback(int id, int user, int book, int rate, String text, Date date) {
14+
public Feedback(int id, int customer, int book, int rate, String text, Date date) {
1515
this.id = id;
16-
this.user = user;
16+
this.customer = customer;
1717
this.book = book;
1818
this.rate = rate;
1919
this.text = text;
@@ -24,8 +24,8 @@ public int getId() {
2424
return id;
2525
}
2626

27-
public int getUser() {
28-
return user;
27+
public int getCustomer() {
28+
return customer;
2929
}
3030

3131
public int getBook() {
@@ -48,7 +48,7 @@ public Date getDate() {
4848
public String toString() {
4949
return "Feedback{" +
5050
"id=" + id +
51-
", user=" + user +
51+
", customer=" + customer +
5252
", book=" + book +
5353
", rate=" + rate +
5454
", text='" + text + '\'' +

‎src/main/java/com/example/randombook/feedback/FeedbackController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.randombook.feedback;
22

3-
import com.example.randombook.category.Category;
43
import org.springframework.http.HttpStatus;
54
import org.springframework.web.bind.annotation.*;
65

@@ -22,9 +21,9 @@ public List<Feedback> findAll() {
2221
return dao.findAll();
2322
}
2423

25-
@GetMapping("/user/{id_user}")
26-
public List<Feedback> findAllByUserId(@PathVariable int id_user) {
27-
return dao.findAllByUserId(id_user);
24+
@GetMapping("/customer/{id_customer}")
25+
public List<Feedback> findAllByCustomerId(@PathVariable int id_customer) {
26+
return dao.findAllByCustomerId(id_customer);
2827
}
2928

3029
@GetMapping("/book/{id_book}")

‎src/main/java/com/example/randombook/feedback/FeedbackDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public interface FeedbackDAO {
77
Optional<Feedback> findById(int id);
88
List<Feedback> findAll();
9-
List<Feedback> findAllByUserId(int id_user);
9+
List<Feedback> findAllByCustomerId(int id_customer);
1010
List<Feedback> findAllByBookId(int id_book);
1111
Feedback create(Feedback feedback);
1212
Feedback update(Feedback feedback, int id);

‎src/main/java/com/example/randombook/feedback/JdbcFeedbackDAO.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.randombook.feedback;
22

3-
import com.example.randombook.category.Category;
43
import org.springframework.jdbc.core.JdbcTemplate;
54
import org.springframework.jdbc.core.RowMapper;
65
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
@@ -26,7 +25,7 @@ public JdbcFeedbackDAO(JdbcTemplate jdbcTemplate) {
2625

2726
RowMapper<Feedback> rowMapper = (rs, rowNum) -> new Feedback(
2827
rs.getInt("id_feedback"),
29-
rs.getInt("id_user"),
28+
rs.getInt("id_customer"),
3029
rs.getInt("id_book"),
3130
rs.getInt("rate"),
3231
rs.getString("text"),
@@ -46,9 +45,9 @@ public List<Feedback> findAll() {
4645
}
4746

4847
@Override
49-
public List<Feedback> findAllByUserId(int id_user) {
50-
String sql = "SELECT * FROM feedback where id_user = ?";
51-
return jdbcTemplate.query(sql, rowMapper, id_user);
48+
public List<Feedback> findAllByCustomerId(int id_customer) {
49+
String sql = "SELECT * FROM feedback where id_customer = ?";
50+
return jdbcTemplate.query(sql, rowMapper, id_customer);
5251
}
5352

5453
@Override
@@ -60,15 +59,15 @@ public List<Feedback> findAllByBookId(int id_book) {
6059
@Override
6160
public Feedback create(Feedback feedback) {
6261
Map<String,Object> parameters = new HashMap<>();
63-
parameters.put("id_user", feedback.getUser());
62+
parameters.put("id_customer", feedback.getCustomer());
6463
parameters.put("id_book", feedback.getBook());
6564
parameters.put("rate", feedback.getRate());
6665
parameters.put("text", feedback.getText());
6766
parameters.put("date", feedback.getDate());
6867
Number id_feedback = insertFeedback.executeAndReturnKey(parameters);
6968
return new Feedback(
7069
(Integer) id_feedback,
71-
feedback.getUser(),
70+
feedback.getCustomer(),
7271
feedback.getBook(),
7372
feedback.getRate(),
7473
feedback.getText(),
@@ -79,21 +78,21 @@ public Feedback create(Feedback feedback) {
7978
@Override
8079
public Feedback update(Feedback feedback, int id) {
8180
String sql = "update feedback set " +
82-
"id_user = ?," +
81+
"id_customer = ?," +
8382
"id_book = ?, rate = ?," +
8483
"text = ?, date = ? " +
8584
"where id_feedback = ?";
8685
Feedback f = new Feedback(
8786
id,
88-
feedback.getUser(),
87+
feedback.getCustomer(),
8988
feedback.getBook(),
9089
feedback.getRate(),
9190
feedback.getText(),
9291
feedback.getDate()
9392
);
9493
jdbcTemplate.update(
9594
sql,
96-
f.getUser(),
95+
f.getCustomer(),
9796
f.getBook(),
9897
f.getRate(),
9998
f.getText(),

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /