-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from 1Cezzo/implement-quiz
Quiz model, repository and service, many to many with user
- Loading branch information
Showing
7 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/idatt2105/backend/controller/QuizController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.idatt2105.backend.controller; | ||
|
||
import com.idatt2105.backend.model.Quiz; | ||
import com.idatt2105.backend.service.QuizService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("/api/quizzes") | ||
@Tag(name = "Quizzes", description = "Operations related to quizzes") | ||
public class QuizController { | ||
|
||
private final QuizService quizService; | ||
|
||
@Autowired | ||
public QuizController(QuizService quizService) { | ||
this.quizService = quizService; | ||
} | ||
|
||
@GetMapping | ||
@Operation(summary = "Get all quizzes") | ||
public ResponseEntity<List<Quiz>> getAllQuizzes() { | ||
List<Quiz> quizzes = quizService.getAllQuizzes(); | ||
return new ResponseEntity<>(quizzes, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@Operation(summary = "Get quiz by id") | ||
public ResponseEntity<Quiz> getQuizById(@PathVariable("id") Long id) { | ||
Optional<Quiz> quiz = quizService.getQuizById(id); | ||
return quiz.map(value -> new ResponseEntity<>(value, HttpStatus.OK)) | ||
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); | ||
} | ||
|
||
@PostMapping | ||
@Operation(summary = "Create quiz") | ||
public ResponseEntity<Quiz> createQuiz(@RequestBody Quiz quiz) { | ||
Quiz createdQuiz = quizService.saveQuiz(quiz); | ||
return new ResponseEntity<>(createdQuiz, HttpStatus.CREATED); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@Operation(summary = "Delete quiz") | ||
public ResponseEntity<Void> deleteQuiz(@PathVariable("id") Long id) { | ||
quizService.deleteQuiz(id); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@PostMapping("/{quizId}/users/{userId}") | ||
@Operation(summary = "Add user to quiz") | ||
public ResponseEntity<Void> addUserToQuiz(@PathVariable("quizId") Long quizId, @PathVariable("userId") Long userId) { | ||
quizService.addUserToQuiz(userId, quizId); | ||
return new ResponseEntity<>(HttpStatus.CREATED); | ||
} | ||
|
||
@DeleteMapping("/{quizId}/users/{userId}") | ||
@Operation(summary = "Remove user from quiz") | ||
public ResponseEntity<Void> removeUserFromQuiz(@PathVariable("quizId") Long quizId, @PathVariable("userId") Long userId) { | ||
quizService.removeUserFromQuiz(userId, quizId); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.idatt2105.backend.model; | ||
|
||
import lombok.Data; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToMany; | ||
import jakarta.persistence.Table; | ||
import jakarta.validation.constraints.NotEmpty; | ||
|
||
/** | ||
* Represents a quiz. | ||
*/ | ||
@Entity | ||
@Data | ||
@Table(name = "quizzes") | ||
public class Quiz { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotEmpty | ||
@Column(name = "title", nullable = false) | ||
private String title; | ||
|
||
@Column(name = "description", columnDefinition = "TEXT") | ||
private String description; | ||
|
||
@Column(name = "creation_date") | ||
private LocalDateTime creationDate; | ||
|
||
@Column(name = "last_modified_date") | ||
private LocalDateTime lastModifiedDate; | ||
|
||
@ManyToMany(mappedBy = "quizzes") | ||
private Set<User> users = new HashSet<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
package com.idatt2105.backend.model; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinTable; | ||
import jakarta.persistence.ManyToMany; | ||
import jakarta.persistence.Table; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.persistence.JoinColumn; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* Represents a user. | ||
*/ | ||
@Entity | ||
@Data | ||
@Table(name = "users") | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@NotEmpty | ||
@Column(name = "username", nullable = false) | ||
private String username; | ||
@NotEmpty | ||
@Column(name = "password", nullable = false) | ||
private String password; | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotEmpty | ||
@Column(name = "username", nullable = false) | ||
private String username; | ||
|
||
@NotEmpty | ||
@Column(name = "password", nullable = false) | ||
private String password; | ||
|
||
@ManyToMany(cascade = CascadeType.ALL) | ||
@JoinTable(name = "user_quiz", | ||
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), | ||
inverseJoinColumns = @JoinColumn(name = "quiz_id", referencedColumnName = "id")) | ||
private Set<Quiz> quizzes = new HashSet<>(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/idatt2105/backend/repositories/QuizRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.idatt2105.backend.repositories; | ||
|
||
import com.idatt2105.backend.model.Quiz; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface QuizRepository extends JpaRepository<Quiz, Long> { | ||
Optional <Quiz> findByTitle(String title); | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/idatt2105/backend/service/QuizService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.idatt2105.backend.service; | ||
|
||
import com.idatt2105.backend.model.Quiz; | ||
import com.idatt2105.backend.model.User; | ||
import com.idatt2105.backend.repositories.QuizRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class QuizService { | ||
|
||
private final QuizRepository quizRepository; | ||
private final UserService userService; | ||
|
||
@Autowired | ||
public QuizService(QuizRepository quizRepository, UserService userService) { | ||
this.quizRepository = quizRepository; | ||
this.userService = userService; | ||
} | ||
|
||
public List<Quiz> getAllQuizzes() { | ||
return quizRepository.findAll(); | ||
} | ||
|
||
public Optional<Quiz> getQuizById(Long id) { | ||
return quizRepository.findById(id); | ||
} | ||
|
||
public Quiz saveQuiz(Quiz quiz) { | ||
return quizRepository.save(quiz); | ||
} | ||
|
||
public void deleteQuiz(Long id) { | ||
quizRepository.deleteById(id); | ||
} | ||
|
||
public void addUserToQuiz(Long userId, Long quizId) { | ||
Optional<Quiz> quizOptional = quizRepository.findById(quizId); | ||
if (quizOptional.isPresent()) { | ||
Quiz quiz = quizOptional.get(); | ||
User user = userService.getUserById(userId); | ||
if (user != null) { | ||
quiz.getUsers().add(user); | ||
quizRepository.save(quiz); | ||
} else { | ||
// Handle user not found | ||
} | ||
} else { | ||
// Handle quiz not found | ||
} | ||
} | ||
|
||
public void removeUserFromQuiz(Long userId, Long quizId) { | ||
Optional<Quiz> quizOptional = quizRepository.findById(quizId); | ||
if (quizOptional.isPresent()) { | ||
Quiz quiz = quizOptional.get(); | ||
User user = userService.getUserById(userId); | ||
if (user != null) { | ||
quiz.getUsers().remove(user); | ||
quizRepository.save(quiz); | ||
} else { | ||
// Handle user not found | ||
} | ||
} else { | ||
// Handle quiz not found | ||
} | ||
} | ||
|
||
public Optional<Quiz> getQuizByTitle(String title) { | ||
return quizRepository.findByTitle(title); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters