Linking front for forum creation
This commit is contained in:
		| @ -24,6 +24,7 @@ import ovh.herisson.Clyde.Services.AuthenticatorService; | |||||||
| import ovh.herisson.Clyde.Services.CourseService; | import ovh.herisson.Clyde.Services.CourseService; | ||||||
| import ovh.herisson.Clyde.Services.Msg.ForumService; | import ovh.herisson.Clyde.Services.Msg.ForumService; | ||||||
| import ovh.herisson.Clyde.Tables.Course; | import ovh.herisson.Clyde.Tables.Course; | ||||||
|  | import ovh.herisson.Clyde.Tables.Role; | ||||||
| import ovh.herisson.Clyde.Tables.Token; | import ovh.herisson.Clyde.Tables.Token; | ||||||
| import ovh.herisson.Clyde.Tables.User; | import ovh.herisson.Clyde.Tables.User; | ||||||
| import ovh.herisson.Clyde.Tables.Msg.Forum; | import ovh.herisson.Clyde.Tables.Msg.Forum; | ||||||
| @ -46,7 +47,7 @@ public class ForumController { | |||||||
| 	@GetMapping("/forums/{id}") | 	@GetMapping("/forums/{id}") | ||||||
| 	public ResponseEntity<List<Forum>> getForumFromCourseId(@RequestHeader("Authorization") String token, @PathVariable long id){ | 	public ResponseEntity<List<Forum>> getForumFromCourseId(@RequestHeader("Authorization") String token, @PathVariable long id){ | ||||||
| 		User u = authServ.getUserFromToken(token); | 		User u = authServ.getUserFromToken(token); | ||||||
| 		if(u != null){ | 		if(u == null){ | ||||||
| 			return new UnauthorizedResponse<>(null); | 			return new UnauthorizedResponse<>(null); | ||||||
| 		} | 		} | ||||||
| 		return new ResponseEntity<>(courseRepo.findById(id).getForums(), HttpStatus.OK); | 		return new ResponseEntity<>(courseRepo.findById(id).getForums(), HttpStatus.OK); | ||||||
| @ -56,7 +57,7 @@ public class ForumController { | |||||||
| 	public ResponseEntity<Forum> createForumOfCourse(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Forum data){ | 	public ResponseEntity<Forum> createForumOfCourse(@RequestHeader("Authorization") String token, @PathVariable long id, @RequestBody Forum data){ | ||||||
| 		User u = authServ.getUserFromToken(token); | 		User u = authServ.getUserFromToken(token); | ||||||
| 		Course c = courseRepo.findById(id); | 		Course c = courseRepo.findById(id); | ||||||
| 		if(!c.getOwner().equals(u)){ | 		if(!(c.getOwner().equals(u) || u.getRole() == Role.Admin)){ | ||||||
| 			return new UnauthorizedResponse<>(null); | 			return new UnauthorizedResponse<>(null); | ||||||
| 		} | 		} | ||||||
| 		forumServ.createForum(c, data); | 		forumServ.createForum(c, data); | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| package ovh.herisson.Clyde.EndPoints.Msg; | package ovh.herisson.Clyde.Repositories.Msg; | ||||||
|  |  | ||||||
| import org.springframework.data.repository.CrudRepository; | import org.springframework.data.repository.CrudRepository; | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package ovh.herisson.Clyde.Services.Msg; | package ovh.herisson.Clyde.Services.Msg; | ||||||
|  |  | ||||||
| import org.springframework.beans.factory.annotation.Autowired; |  | ||||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||||
|  |  | ||||||
| import lombok.AllArgsConstructor; | import lombok.AllArgsConstructor; | ||||||
|  | |||||||
| @ -24,7 +24,7 @@ public class Course { | |||||||
|     private User owner; |     private User owner; | ||||||
|  |  | ||||||
| 	//// Extension Messagerie ///// | 	//// Extension Messagerie ///// | ||||||
| 	@OneToMany | 	@OneToMany(cascade = CascadeType.ALL) | ||||||
| 	private List<Forum> forums; | 	private List<Forum> forums; | ||||||
|  |  | ||||||
| 	public void addForum(Forum f){ | 	public void addForum(Forum f){ | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ public class Forum { | |||||||
| 	@GeneratedValue(strategy = GenerationType.AUTO) | 	@GeneratedValue(strategy = GenerationType.AUTO) | ||||||
| 	private int id; | 	private int id; | ||||||
|  |  | ||||||
| 	@ManyToOne(cascade = CascadeType.ALL) | 	@ManyToOne | ||||||
| 	private Course course; | 	private Course course; | ||||||
|  |  | ||||||
| 	private String name; | 	private String name; | ||||||
|  | |||||||
| @ -8,13 +8,18 @@ | |||||||
| <script setup> | <script setup> | ||||||
| import { ref, reactive } from 'vue' | import { ref, reactive } from 'vue' | ||||||
| import { getCourses } from '@/rest/courses.js' | import { getCourses } from '@/rest/courses.js' | ||||||
| import { ForumsOfCurrentCourse, getForumsOfCourse } from '@/rest/forum.js' | import { ForumsOfCurrentCourse, getForumsOfCourse, createForum } from '@/rest/forum.js' | ||||||
| import { PostsOfCurrentForum, getPostsOfForum } from '@/rest/forum.js' | import { PostsOfCurrentForum, getPostsOfForum } from '@/rest/forum.js' | ||||||
| import { fetchedPost, fetchPost } from '@/rest/forum.js' | import { fetchedPost, fetchPost } from '@/rest/forum.js' | ||||||
|  | import { getSelf } from '@/rest/Users.js' | ||||||
|  |  | ||||||
| const courses = await reactive(getCourses()); | const courses = await reactive(getCourses()); | ||||||
| const selectedCourse = ref(); | const selectedCourse = ref(); | ||||||
| const selectedForum = ref(); | const selectedForum = ref(); | ||||||
|  | const Role = (await getSelf()).role; | ||||||
|  |  | ||||||
|  | const addForum = ref(false); | ||||||
|  | const addForumName = ref(""); | ||||||
|  |  | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
| @ -28,10 +33,11 @@ const selectedForum = ref(); | |||||||
| 			<select id="forum" value="" v-model="selectedForum"  @change="getPostsOfForum(selectedForum)" v-if="ForumsOfCurrentCourse != null"> | 			<select id="forum" value="" v-model="selectedForum"  @change="getPostsOfForum(selectedForum)" v-if="ForumsOfCurrentCourse != null"> | ||||||
| 				<option v-for="forum in ForumsOfCurrentCourse" :value=forum.id>{{forum.name}}</option> | 				<option v-for="forum in ForumsOfCurrentCourse" :value=forum.id>{{forum.name}}</option> | ||||||
| 			</select> | 			</select> | ||||||
|  | 			<button v-if="(Role === 'Admin' || Role === 'Teacher') && ForumsOfCurrentCourse != null " id="createPost" @click="addForum = true">+</button> | ||||||
| 		</div> | 		</div> | ||||||
| 		<div id="PostSelector" v-if="PostsOfCurrentForum != null"> | 		<div id="PostSelector" v-if="PostsOfCurrentForum != null"> | ||||||
| 			<div @click="fetchPost(post.id)" class="postItem" v-for="post in PostsOfCurrentForum" :key="post.id">{{ post.name }}</div> | 			<div @click="fetchPost(post.id)" class="postItem" v-for="post in PostsOfCurrentForum" :key="post.id">{{ post.name }}</div> | ||||||
| 			<!--button id="createPost" @click="createPost()">+</button--> | 			<button v-if="Role === 'Admin' || Role === 'Teacher' "  id="createPost" @click="createPost()">+</button> | ||||||
| 		</div> | 		</div> | ||||||
| 		<div id="PostViewer" v-if="fetchedPost != null"> | 		<div id="PostViewer" v-if="fetchedPost != null"> | ||||||
| 			<div id="Post"> | 			<div id="Post"> | ||||||
| @ -43,10 +49,41 @@ const selectedForum = ref(); | |||||||
| 			</div> | 			</div> | ||||||
| 		</div> | 		</div> | ||||||
| 	</div> | 	</div> | ||||||
|  | 	<div id="forumAdder" v-if=addForum @click.self="addForum = false"> | ||||||
|  | 		<div id="addForumForm"> | ||||||
|  | 			<label>New Forum:</label> | ||||||
|  | 			<input type="text" placeholder="Name" v-model=addForumName @keyup.enter="createForum(selectedCourse, $event.target.value); addForum = false;" /> | ||||||
|  | 		</div> | ||||||
|  | 	</div> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <style scoped> | <style scoped> | ||||||
|  |  | ||||||
|  | #forumAdder{ | ||||||
|  | 	position: fixed; | ||||||
|  | 	top: 0; | ||||||
|  | 	left: 0; | ||||||
|  | 	width: 100vw; | ||||||
|  | 	height: 100vh; | ||||||
|  | 	background-color: #00000022; | ||||||
|  | 	z-index: 9; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #addForumForm{ | ||||||
|  | 	position: relative; | ||||||
|  | 	width: 30%; | ||||||
|  | 	left: calc(50% - 30% / 2); | ||||||
|  | 	top: calc(50% - 10% / 2); | ||||||
|  | 	border-radius: 10px; | ||||||
|  | 	height: 10%; | ||||||
|  | 	background-color: white; | ||||||
|  |  | ||||||
|  | 	display: flex; | ||||||
|  | 	justify-content: center; | ||||||
|  | 	align-items: center; | ||||||
|  | 	gap: 10px; | ||||||
|  | } | ||||||
|  |  | ||||||
| #app{ | #app{ | ||||||
| 	display: grid; | 	display: grid; | ||||||
| 	width: 100%; | 	width: 100%; | ||||||
|  | |||||||
| @ -5,21 +5,15 @@ import { restGet, restPost, restDelete, restPatch } from './restConsumer.js' | |||||||
|  * List forums of a course |  * List forums of a course | ||||||
|  */ |  */ | ||||||
| export async function getForumsOfCourse(id){ | export async function getForumsOfCourse(id){ | ||||||
| 	ForumsOfCurrentCourse.value = [ | 	ForumsOfCurrentCourse.value = await restGet("/forums/" + id) | ||||||
| 		{ |  | ||||||
| 			id: 1, |  | ||||||
| 			name: "forum~1"  |  | ||||||
| 		}, |  | ||||||
| 		{ |  | ||||||
| 			id: 2, |  | ||||||
| 			name: "forum~2"  |  | ||||||
| 		}, |  | ||||||
| 	] |  | ||||||
| 	// ForumsOfCurrentCourse = await restGet("/forums/" + id) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| export const ForumsOfCurrentCourse = ref(); | export const ForumsOfCurrentCourse = ref(); | ||||||
|  |  | ||||||
|  | export function createForum(id, name){ | ||||||
|  | 	restPost("/forums/" + id, {name: name}).then(_ => getForumsOfCourse(id)); | ||||||
|  | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * List post of a specified forum |  * List post of a specified forum | ||||||
|  */ |  */ | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user