Skip to content

Commit 63f67b7

Browse files
author
Rachit Bhasin
committed
Updated build script and created user controller
1 parent 5121616 commit 63f67b7

File tree

19 files changed

+294
-19
lines changed

19 files changed

+294
-19
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@
179179
</execution>
180180

181181
<execution>
182-
<id>yarn run start</id>
182+
<id>yarn run build:dev</id>
183183
<goals>
184184
<goal>yarn</goal>
185185
</goals>
186186
<configuration>
187-
<arguments>run start</arguments>
187+
<arguments>run build:dev</arguments>
188188
</configuration>
189189
</execution>
190190
</executions>

src/main/java/com/rc/uam/config/WebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected void configure(HttpSecurity http) throws Exception {
6969
.exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint).and()
7070
.authorizeRequests()
7171
.antMatchers("/static/**").permitAll()
72-
.antMatchers("/app/**").permitAll()
72+
.antMatchers("/**").permitAll()
7373
.antMatchers("/auth/login").permitAll()
7474
.anyRequest().authenticated().and()
7575
.addFilterBefore(new TokenAuthenticationFilter(tokenHelper, jwtUserDetailsService), BasicAuthenticationFilter.class);

src/main/java/com/rc/uam/controller/HomeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class HomeController {
1515

1616
private final Logger logger = Logger.getLogger(this.getClass());
1717

18-
@RequestMapping(value = { "/app/*", "/app/*/*" }, method = RequestMethod.GET)
18+
@RequestMapping(value = { "/*", "/*/*" }, method = RequestMethod.GET)
1919
public String homePage(ModelMap model) {
2020
logger.info("Loading index page for SPA application");
2121

src/main/java/com/rc/uam/rest/AuthenticationController.java renamed to src/main/java/com/rc/uam/controller/rest/AuthenticationController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.rc.uam.rest;
1+
package com.rc.uam.controller.rest;
22

33

44
import java.io.IOException;

src/main/java/com/rc/uam/rest/BookController.java renamed to src/main/java/com/rc/uam/controller/rest/BookController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.rc.uam.rest;
1+
package com.rc.uam.controller.rest;
22

33
import java.util.List;
44

@@ -47,7 +47,7 @@ public ResponseEntity<Book> get(@PathVariable("id") long id) {
4747

4848
/*---get all books---*/
4949
@GetMapping("/book")
50-
@PreAuthorize("hasRole('USER')")
50+
@PreAuthorize("hasAnyRole('ADMIN','USER')")
5151
public ResponseEntity<List<Book>> list() {
5252
List<Book> books = bookService.list();
5353
return ResponseEntity.ok().body(books);

src/main/java/com/rc/uam/rest/RestExceptionController.java renamed to src/main/java/com/rc/uam/controller/rest/RestExceptionController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.rc.uam.rest;
1+
package com.rc.uam.controller.rest;
22

33
import org.springframework.http.HttpStatus;
44
import org.springframework.web.bind.annotation.ControllerAdvice;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.rc.uam.controller.rest;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.MediaType;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.security.access.prepost.PreAuthorize;
8+
import org.springframework.web.bind.annotation.DeleteMapping;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.PutMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import com.rc.uam.exception.UamException;
18+
import com.rc.uam.model.Book;
19+
import com.rc.uam.model.User;
20+
import com.rc.uam.service.UserService;
21+
22+
/**
23+
* @author Rachit Bhasin
24+
*
25+
*/
26+
@RestController
27+
@RequestMapping( value = "/api", produces = MediaType.APPLICATION_JSON_VALUE )
28+
public class UserController {
29+
@Autowired
30+
UserService userService;
31+
32+
/*---Add new User---*/
33+
@PostMapping("/user")
34+
@PreAuthorize("hasRole('ADMIN')")
35+
public ResponseEntity<String> save(@RequestBody User user) throws UamException {
36+
long id = userService.save(user);
37+
return ResponseEntity.ok().body("New User has been saved with ID:" + id);
38+
}
39+
40+
/*---Get a User by id---*/
41+
@GetMapping("/user/{id}")
42+
@PreAuthorize("hasAnyRole('ADMIN','USER')")
43+
public ResponseEntity<?> get(@PathVariable("id") long id) throws UamException {
44+
User user = userService.get(id);
45+
if(user == null) {
46+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
47+
}
48+
return ResponseEntity.ok().body(user);
49+
}
50+
51+
/*---Update a user by id---*/
52+
@PutMapping("/user/{id}")
53+
@PreAuthorize("hasRole('ADMIN')")
54+
public ResponseEntity<?> update(@PathVariable("id") long id, @RequestBody User user) throws UamException {
55+
userService.update(id, user);
56+
return ResponseEntity.ok().body("Book has been updated successfully.");
57+
}
58+
59+
/*---Delete a user by id---*/
60+
@DeleteMapping("/user/{id}")
61+
@PreAuthorize("hasRole('ADMIN')")
62+
public ResponseEntity<?> delete(@PathVariable("id") long id) throws UamException {
63+
userService.delete(id);
64+
return ResponseEntity.ok().body("Book has been deleted successfully.");
65+
}
66+
}

src/main/java/com/rc/uam/service/UserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public interface UserService {
1313
long save(User user) throws UamException;
14-
User find(Long id) throws UamException;
14+
User get(Long id) throws UamException;
1515
User findByField(String field, String value) throws UamException;
1616
List<User> list() throws UamException;
1717
void update(Long id, User user) throws UamException;

src/main/java/com/rc/uam/service/impl/UserServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public long save(User user) throws UamException {
3131
}
3232

3333
@Override
34-
public User find(Long id) throws UamException {
34+
public User get(Long id) throws UamException {
3535
return userDao.get(id);
3636
}
3737

src/main/resources/db.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# MySQL properties
22
mysql.driver=com.mysql.cj.jdbc.Driver
33
mysql.url=jdbc:mysql://localhost:3306/uam
4-
mysql.user=rachit.bhasin
5-
mysql.password=login@123
4+
mysql.user=root
5+
mysql.password=root
66

77
# Hibernate properties
88
hibernate.show_sql=true
99
hibernate.format_sql = false
10-
#hibernate.hbm2ddl.auto=create
11-
hibernate.hbm2ddl.auto=update
10+
hibernate.hbm2ddl.auto=create
11+
# hibernate.hbm2ddl.auto=update
1212

1313
#C3P0 properties
1414
hibernate.c3p0.min_size=5

0 commit comments

Comments
 (0)