Commit 729d9088 authored by Mamadou Diallo's avatar Mamadou Diallo
Browse files

User CRUD done

parent 6cd521ac
......@@ -17,10 +17,12 @@
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......@@ -52,11 +54,13 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
......
......@@ -4,16 +4,18 @@ import com.example.server.model.IUser;
import com.example.server.model.User;
import com.example.server.utils.Const;
import com.example.server.utils.PasswordEncoderHelper;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.example.server.utils.Const.HOME;
@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
private final List<IUser> users = Stream.of(
......@@ -24,18 +26,10 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
/*
users.stream().map(user -> {
try {
auth.inMemoryAuthentication().withUser(user.getUserName())
.password(PasswordEncoderHelper.passwordEncoder().encode(user.getPassword()))
.roles(user.getRoles());
} catch (Exception e) {
e.printStackTrace();
}
return null;
});
*/
User user = new User("Diallo", "Mamadou", "greer", "Greer", Const.ADMIN);
auth.inMemoryAuthentication().withUser(user.getUserName())
.password(PasswordEncoderHelper.passwordEncoder().encode(user.getPassword()))
.roles(user.getRoles());
}
@Override
......@@ -43,9 +37,13 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
http
.csrf().disable()
.authorizeRequests()
//.antMatchers("/api/admin/**").hasRole(Const.ADMIN)
//.antMatchers("/api/home").permitAll()
.antMatchers("/api/admin/**").hasRole(Const.ADMIN)
.antMatchers("/api/home").permitAll()
.antMatchers("/api/user/add").permitAll()
.antMatchers("/api/user/**").permitAll()
//.antMatchers("/api/login*").permitAll()
.anyRequest().authenticated();
}
}
......@@ -3,7 +3,9 @@ package com.example.server.controller;
import com.example.server.exceptions.CommentNotFoundException;
import com.example.server.model.Comment;
import com.example.server.repository.CommentRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
......@@ -16,10 +18,13 @@ import java.util.Optional;
public class CommentController {
private final CommentRepository repository;
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add")
public Comment add(@RequestBody JsonNode jsonNode) {
return null;
public Comment add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
Comment comment = mapper.treeToValue(jsonNode, Comment.class);
//System.out.println("comment: "+comment.toString());
return comment;
}
@PutMapping("/{id}")
......@@ -34,6 +39,7 @@ public class CommentController {
@GetMapping("/{id}")
public Optional<Comment> getById(@PathVariable String id) {
return repository.findById(id);
}
......
......@@ -12,4 +12,9 @@ public class Controller {
public String homePage(){
return "Welcome";
}
@GetMapping("/admin")
public String adminHome(){
return "Admin page";
}
}
......@@ -3,8 +3,12 @@ package com.example.server.controller;
import com.example.server.exceptions.UserNotFoundException;
import com.example.server.model.User;
import com.example.server.repository.UserRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -16,37 +20,51 @@ import java.util.Optional;
public class UserController {
private final UserRepository repository;
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add")
public User add(@RequestBody JsonNode jsonNode) {
return null;
public User add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
User user = mapper.treeToValue(jsonNode, User.class);
repository.save(user);
return user;
}
@PutMapping("/{id}")
public User update(@PathVariable String idUser, @RequestBody JsonNode jsonNode) {
if(!repository.existsById(idUser)){
throw new UserNotFoundException(idUser);
public ResponseEntity<User> update(@PathVariable("id") String id, @RequestBody JsonNode jsonNode) throws JsonProcessingException {
if(!repository.existsById(id)){
throw new UserNotFoundException(id);
}
repository.deleteById(idUser);
//return userRepository.save(newUser);
return null;
repository.deleteById(id);
User user = mapper.treeToValue(jsonNode, User.class);
return new ResponseEntity<>(repository.save(user), HttpStatus.OK);
}
@GetMapping("/{id}")
public Optional<User> getById(@PathVariable String id) {
return repository.findById(id);
public ResponseEntity<User> getById(@PathVariable("id") String id) {
System.out.println("id : "+id);
Optional<User> user = repository.findById(id);
return user.map(value -> new ResponseEntity<>(value, HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable String id) {
public ResponseEntity<?> deleteById(@PathVariable String id) {
if(!repository.existsById(id)){
throw new UserNotFoundException(id);
}
repository.deleteById(id);
return new ResponseEntity<>("Deleted successfully !", HttpStatus.OK);
}
@GetMapping("/all")
public List<User> getAll() {
return repository.findAll();
}
@DeleteMapping("/all")
public ResponseEntity<?> deleteAll(){
if(repository.count() > 0){
repository.deleteAll();
}
return ResponseEntity.ok("All users Deleted successfully");
}
}
......@@ -10,6 +10,6 @@ public class UserNotFoundException extends RuntimeException{
private static final long serialVersionUID = 1L;
public UserNotFoundException(String idUser){
super(String.format("No user found with id %s ", idUser));
super(String.format("No user found for id %s ", idUser));
}
}
......@@ -5,7 +5,7 @@ import java.util.UUID;
public interface IUser {
UUID getId();
String getId();
String getFirstName();
String getLastName();
String getUserName();
......
package com.example.server.model;
import lombok.Data;
import lombok.*;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
......@@ -9,6 +9,10 @@ import java.util.List;
import java.util.Objects;
@Data
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "post")
public class Post extends Common{
......
package com.example.server.model;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
@Data
@Setter
@Getter
@NoArgsConstructor
@Document(collection = "user")
public class User implements IUser {
public class User implements IUser{
private UUID id;
@Id
private String id;
@Field("firstName")
private String firstName;
@Field("lastName")
private String lastName;
@Field("userName")
private String userName;
@Field("password")
private String password;
@Field("role")
private String role;
public User(String firstName, String lastName, String userName, String password, String role) {
this.id = UUID.randomUUID();
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
......@@ -30,7 +40,7 @@ public class User implements IUser {
}
@Override
public UUID getId() {
public String getId() {
return id;
}
......@@ -114,4 +124,5 @@ public class User implements IUser {
public int hashCode() {
return Objects.hash(getId(), getFirstName(), getLastName(), getUserName(), getRoles());
}
}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment