Add moderator module with routing, components, and API integration
This commit is contained in:
@@ -66,30 +66,6 @@ public class TutorialController {
|
||||
try {
|
||||
List<Tutorial> tutorials = new ArrayList<Tutorial>();
|
||||
|
||||
/* Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
String name = authenticationFacade.getAuthentication().getName();
|
||||
|
||||
User user = userRepository.findById(((UserDetailsImpl)userDetails).getId()).get();
|
||||
if (user.getRoles().stream().anyMatch(role -> role.getName().name().equals("ROLE_ADMIN"))) {*/
|
||||
/*
|
||||
if (title == null)
|
||||
tutorialRepository.findAll().forEach(tutorials::add);
|
||||
else
|
||||
tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);
|
||||
*/
|
||||
|
||||
/* } else {
|
||||
|
||||
// If the user is not an admin, filter tutorials by user
|
||||
tutorialRepository.findByUserId(user.getId()).forEach(tutorials::add);
|
||||
if (tutorials.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
return new ResponseEntity<>(tutorials, HttpStatus.OK);
|
||||
}*/
|
||||
|
||||
//all published tutorials without authentication
|
||||
tutorialRepository.findByPublished(true).forEach(tutorials::add);
|
||||
|
||||
if (tutorials.isEmpty()) {
|
||||
@@ -102,6 +78,23 @@ public class TutorialController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/moderator/tutorials")
|
||||
public ResponseEntity<List<Tutorial>> getBePublishedTutorials(@RequestParam(required = false) String title) {
|
||||
try {
|
||||
List<Tutorial> tutorials = new ArrayList<Tutorial>();
|
||||
|
||||
tutorialRepository.findBytobepublished(true).forEach(tutorials::add);
|
||||
|
||||
if (tutorials.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(tutorials, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("user/tutorial-add")
|
||||
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {
|
||||
|
||||
@@ -111,8 +104,18 @@ public class TutorialController {
|
||||
User user = userRepository.findById(((UserDetailsImpl)userDetails).getId()).get();
|
||||
|
||||
try {
|
||||
Tutorial newTutorial =new Tutorial(
|
||||
tutorial.getTitle(),
|
||||
tutorial.getDescription(),
|
||||
false,
|
||||
false,
|
||||
user,
|
||||
Timestamp.valueOf(LocalDateTime.now()),
|
||||
Timestamp.valueOf(LocalDateTime.now())
|
||||
);
|
||||
|
||||
Tutorial _tutorial = tutorialRepository
|
||||
.save(new Tutorial(tutorial.getTitle(), tutorial.getDescription(), false, user, Timestamp.valueOf(LocalDateTime.now()), Timestamp.valueOf(LocalDateTime.now())));
|
||||
.save(newTutorial);
|
||||
return new ResponseEntity<>(_tutorial, HttpStatus.CREATED);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
@@ -131,6 +134,18 @@ public class TutorialController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("moderator/tutorial-get/{id}")
|
||||
public ResponseEntity<Tutorial> getBePublishedTutorial(@PathVariable("id") long id) {
|
||||
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
|
||||
|
||||
if (tutorialData.isPresent()) {
|
||||
|
||||
return new ResponseEntity<Tutorial>(tutorialData.get(), HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping("user/tutorial-update/{id}")
|
||||
public ResponseEntity<?> updateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial) {
|
||||
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
|
||||
@@ -140,6 +155,31 @@ public class TutorialController {
|
||||
servTutorial.setTitle(tutorial.getTitle());
|
||||
servTutorial.setDescription(tutorial.getDescription());
|
||||
servTutorial.setPublished(tutorial.isPublished());
|
||||
servTutorial.setTobepublished(tutorial.isTobepublished());
|
||||
try {
|
||||
servTutorial = tutorialRepository.save(servTutorial);
|
||||
} catch (Exception e) {
|
||||
|
||||
map.put("status", 0);
|
||||
map.put("message", e.getMessage());
|
||||
return new ResponseEntity<>(map,HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return new ResponseEntity<>(servTutorial, HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping("moderator/tutorial-update/{id}")
|
||||
public ResponseEntity<?> moderatorUpdateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial) {
|
||||
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
if (tutorialData.isPresent()) {
|
||||
Tutorial servTutorial = tutorialData.get();
|
||||
servTutorial.setTitle(tutorial.getTitle());
|
||||
servTutorial.setDescription(tutorial.getDescription());
|
||||
servTutorial.setPublished(tutorial.isPublished());
|
||||
servTutorial.setTobepublished(tutorial.isTobepublished());
|
||||
try {
|
||||
servTutorial = tutorialRepository.save(servTutorial);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.jambotronGroup.jambotron.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
|
||||
@Entity
|
||||
@@ -21,6 +22,9 @@ public class Tutorial {
|
||||
@Column(name = "published")
|
||||
private boolean published;
|
||||
|
||||
@Column(name = "tobepublished")
|
||||
private boolean tobepublished;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "userID", nullable = false)
|
||||
private User user;
|
||||
@@ -37,10 +41,11 @@ public class Tutorial {
|
||||
|
||||
}
|
||||
|
||||
public Tutorial(String title, String description, boolean published, User user, java.sql.Timestamp created, java.sql.Timestamp modified) {
|
||||
public Tutorial(String title, String description, boolean published, boolean tobepublished, User user, Timestamp created, Timestamp modified) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.published = published;
|
||||
this.tobepublished = tobepublished;
|
||||
this.user = user;
|
||||
this.created = created;
|
||||
this.modified = modified;
|
||||
@@ -74,6 +79,14 @@ public class Tutorial {
|
||||
this.published = isPublished;
|
||||
}
|
||||
|
||||
public boolean isTobepublished() {
|
||||
return tobepublished;
|
||||
}
|
||||
|
||||
public void setTobepublished(boolean tobepublished) {
|
||||
this.tobepublished = tobepublished;
|
||||
}
|
||||
|
||||
public void setCreated(java.sql.Timestamp created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.Set;
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "username")
|
||||
|
||||
@@ -14,5 +14,7 @@ public interface TutorialRepository extends JpaRepository<Tutorial, Long> {
|
||||
List<Tutorial> findByUserId(Long userId);
|
||||
List<Tutorial> findByUserIdAndTitle(Long userId, String title);
|
||||
List<Tutorial> findByPublished(boolean published);
|
||||
List<Tutorial> findBytobepublished(boolean tobepublished);
|
||||
List<Tutorial> findByIdAndTobepublished(Long id,boolean tobepublished);
|
||||
List<Tutorial> findByTitleContaining(String title);
|
||||
}
|
||||
@@ -140,7 +140,7 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
|
||||
.requestMatchers("/api/public/tutorials/**").permitAll()
|
||||
|
||||
.requestMatchers("/api/user/**").permitAll()
|
||||
|
||||
.requestMatchers("/api/moderator/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
);
|
||||
|
||||
@@ -160,6 +160,8 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
|
||||
config.addAllowedHeader("*");
|
||||
config.addAllowedMethod("*");
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
|
||||
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ app.origin=http://localhost:4200
|
||||
|
||||
spring.mvc.throw-exception-if-no-handler-found=true
|
||||
|
||||
#spring.mvc.dispatch-options-request=true
|
||||
|
||||
# Disable the default mappings
|
||||
#spring.resources.add-mappings=false
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS roles
|
||||
(
|
||||
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
name character varying(20) COLLATE pg_catalog."default"
|
||||
name character varying(20)
|
||||
|
||||
);
|
||||
|
||||
@@ -4,6 +4,7 @@ CREATE TABLE tutorials
|
||||
(
|
||||
id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
|
||||
published boolean,
|
||||
tobepublished boolean,
|
||||
title character varying(255) COLLATE pg_catalog."default",
|
||||
userid bigint,
|
||||
created timestamp with time zone,
|
||||
|
||||
Reference in New Issue
Block a user