Adding the discussion architectures and creating new discussions
This commit is contained in:
		| @ -16,6 +16,8 @@ repositories { | ||||
| } | ||||
|  | ||||
| dependencies { | ||||
| 	compileOnly("org.projectlombok:lombok") | ||||
| 	annotationProcessor("org.projectlombok:lombok") | ||||
| 	implementation("org.springframework.boot:spring-boot-starter-jdbc") | ||||
| 	implementation("org.springframework.boot:spring-boot-starter-data-jpa") | ||||
| 	implementation("org.springframework.boot:spring-boot-starter-mail") | ||||
|  | ||||
| @ -0,0 +1,49 @@ | ||||
| package ovh.herisson.Clyde.EndPoints.Msg; | ||||
|  | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.bind.annotation.CrossOrigin; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||
| import org.springframework.web.bind.annotation.PathVariable; | ||||
| import org.springframework.web.bind.annotation.PostMapping; | ||||
| import org.springframework.web.bind.annotation.RequestBody; | ||||
| import org.springframework.web.bind.annotation.RequestHeader; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import lombok.AllArgsConstructor; | ||||
| import ovh.herisson.Clyde.Responses.UnauthorizedResponse; | ||||
| import ovh.herisson.Clyde.Services.AuthenticatorService; | ||||
| import ovh.herisson.Clyde.Services.Msg.DiscussionService; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
| import ovh.herisson.Clyde.Tables.Msg.Discussion; | ||||
|  | ||||
| @RestController | ||||
| @CrossOrigin(originPatterns = "*", allowCredentials = "true") | ||||
| @AllArgsConstructor | ||||
| public class MessagesController { | ||||
|  | ||||
| 	private AuthenticatorService authServ; | ||||
| 	private DiscussionService discServ; | ||||
|  | ||||
| 	@GetMapping("/discussions") | ||||
| 	public ResponseEntity<Iterable<Discussion>> getDiscussions(@RequestHeader("Authorization") String token ){ | ||||
| 		User user = authServ.getUserFromToken(token); | ||||
| 		if(user == null){ | ||||
| 			return new UnauthorizedResponse<>(null); | ||||
| 		} | ||||
|  | ||||
| 		Iterable<Discussion> mock = discServ.getOwned(authServ.getUserFromToken(token)); | ||||
|  | ||||
| 		return new ResponseEntity<>(mock, HttpStatus.OK);  | ||||
| 	}  | ||||
|  | ||||
| 	@GetMapping("/discussion/{id}") | ||||
| 	public ResponseEntity<Discussion> getDiscussion(@RequestHeader("Authorization") String token, @PathVariable long id){ | ||||
| 		return null; // TODO | ||||
| 	}  | ||||
|  | ||||
| 	@PostMapping("/discussion") | ||||
| 	public ResponseEntity<Discussion> createDiscussion(@RequestHeader("Authorization") String token, @RequestBody Discussion data){ | ||||
| 		return new ResponseEntity<>(discServ.create(data.getName(), authServ.getUserFromToken(token)), HttpStatus.OK); | ||||
| 	} | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| package ovh.herisson.Clyde.Repositories.Msg; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import org.springframework.data.jpa.repository.Query; | ||||
| import org.springframework.data.repository.CrudRepository; | ||||
|  | ||||
| import ovh.herisson.Clyde.Tables.Msg.Discussion; | ||||
|  | ||||
| public interface DiscussionRepository extends CrudRepository<Discussion, Long>{ | ||||
|  | ||||
| 	@Query("SELECT d FROM Discussion d INNER JOIN FETCH d.members dm WHERE dm.id = ?1") | ||||
| 	List<Discussion> findByMembership(long userid); | ||||
| } | ||||
| @ -0,0 +1,24 @@ | ||||
| package ovh.herisson.Clyde.Services.Msg; | ||||
|  | ||||
| import org.springframework.stereotype.Service; | ||||
|  | ||||
| import lombok.AllArgsConstructor; | ||||
| import ovh.herisson.Clyde.Repositories.Msg.DiscussionRepository; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
| import ovh.herisson.Clyde.Tables.Msg.Discussion; | ||||
|  | ||||
| @Service | ||||
| @AllArgsConstructor | ||||
| public class DiscussionService { | ||||
|  | ||||
| 	private DiscussionRepository discRepo; | ||||
|  | ||||
| 	public Discussion create(String name, User author){ | ||||
| 		return discRepo.save(new Discussion(name, author)); | ||||
| 	} | ||||
|  | ||||
| 	public Iterable<Discussion> getOwned(User author){ | ||||
| 		return discRepo.findByMembership(author.getRegNo()); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,44 @@ | ||||
| package ovh.herisson.Clyde.Tables.Msg; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import jakarta.persistence.Entity; | ||||
| import jakarta.persistence.GeneratedValue; | ||||
| import jakarta.persistence.GenerationType; | ||||
| import jakarta.persistence.Id; | ||||
| import jakarta.persistence.JoinColumn; | ||||
| import jakarta.persistence.JoinTable; | ||||
| import jakarta.persistence.ManyToMany; | ||||
| import lombok.Getter; | ||||
| import lombok.NoArgsConstructor; | ||||
| import lombok.Setter; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
|  | ||||
| @Entity | ||||
| @Getter | ||||
| @Setter | ||||
| @NoArgsConstructor | ||||
| public class Discussion{ | ||||
|  | ||||
| 	@Id | ||||
|     @GeneratedValue(strategy = GenerationType.AUTO) | ||||
| 	private long id; | ||||
| 	private String name; | ||||
|  | ||||
|     @ManyToMany | ||||
| 	@JoinTable( | ||||
| 		name = "discussion_members", | ||||
| 		joinColumns = @JoinColumn(name = "discussion_id"), | ||||
| 		inverseJoinColumns = @JoinColumn(name = "user_id") | ||||
| 	) | ||||
| 	private List<User> members; | ||||
|  | ||||
| 	public Discussion(String name){ | ||||
| 		this.name = name; | ||||
| 	} | ||||
|  | ||||
| 	public Discussion(String name, User user){ | ||||
| 		this.name = name; | ||||
| 		this.members = List.of(user); | ||||
| 	} | ||||
| } | ||||
| @ -1,7 +1,10 @@ | ||||
| package ovh.herisson.Clyde.Tables; | ||||
|  | ||||
| import jakarta.persistence.*; | ||||
| import ovh.herisson.Clyde.Tables.Msg.Discussion; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
|  | ||||
|  | ||||
| @Entity | ||||
| @ -20,6 +23,10 @@ public class User { | ||||
|     private String profilePictureUrl; | ||||
|     private ovh.herisson.Clyde.Tables.Role role; | ||||
|     private String password; | ||||
|  | ||||
| 	@ManyToMany( mappedBy = "members" ) | ||||
| 	private List<Discussion> discussions; | ||||
|  | ||||
|     public User(String lastName, String firstName, String email, String address, | ||||
|                 String country, Date birthDate, String profilePictureUrl, Role role, String password) | ||||
|     { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user