manque tests

Maxime
Maxime POINT 1 year ago
parent c4f977c45a
commit 808ccbc480

@ -1,5 +1,7 @@
package SAE.ApiREST.WebService.controller;
import SAE.ApiREST.WebService.exception.ArticleException;
import SAE.ApiREST.WebService.exception.CollectException;
import SAE.ApiREST.WebService.model.Article;
import SAE.ApiREST.WebService.model.Collect;
import SAE.ApiREST.WebService.service.ICollectionService;
@ -25,22 +27,34 @@ public class CollectController {
// region GET
@GetMapping(value = "/getAllCollection", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Collect> getAllCollection(){
return collectionService.getAllCollections();
List<Collect> results = collectionService.getAllCollections();
if(results.isEmpty()) {
throw new CollectException("No collections available");
}
@GetMapping(value = "/getCollectionById/{isbn}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Collect getCollectionById(@PathVariable(value = "isbn") long isbn){
return collectionService.getCollectionById(isbn);
return results;
}
@GetMapping(value = "/getCollectionById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Collect getCollectionById(@PathVariable(value = "id") Integer id){
Collect results = collectionService.getCollectionById(id);
if(results == null) {
throw new CollectException("No collections available");
}
return results;
}
@GetMapping(value = "/getAllCollectionsByName/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Collect> getAllCollectionsByName(@PathVariable(value = "name") String name){
return collectionService.getAllCollectionsByName(name);
List<Collect> results = collectionService.getAllCollectionsByName(name);
if(results.isEmpty()) {
throw new CollectException("No collections available");
}
return results;
}
// endregion
// region DELETE
@DeleteMapping(value = "/deleteColletionById/{isbn}")
public @ResponseBody void deleteColletionById(@RequestParam("isbn") long isbn){
collectionService.deleteColletionById(isbn);
@DeleteMapping(value = "/deleteColletionById/{id}")
public @ResponseBody void deleteColletionById(@RequestParam("id") Integer id){
collectionService.deleteColletionById(id);
}
@DeleteMapping(value = "/deleteColletionByName/{name}")
@ -72,17 +86,22 @@ public class CollectController {
collectionService.modifyCollectionName(collection,name);
}
@PostMapping(value="/modifyCollectionNameById", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void modifyCollectionNameById(@RequestParam("isbn") long isbn, @RequestParam("name") String name){
collectionService.modifyCollectionNameById(isbn,name);
@PostMapping(value="/modifyCollectionNameById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") String name){
collectionService.modifyCollectionNameById(id,name);
}
// endregion
// endregion
// region Article
@GetMapping(value = "/getAllArticles", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getAllArticles(@RequestParam("collection") Collect collection){
return collectionService.getAllArticles(collection);
@GetMapping(value = "/getAllArticlesById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getAllArticlesById(@PathVariable(value = "id") Integer id){
List<Article> results = collectionService.getAllArticlesById(id);
if(results == null) {
throw new ArticleException("No articles available");
}
return results;
}
@PutMapping(value = "/addArticle", produces = MediaType.APPLICATION_JSON_VALUE)

@ -19,19 +19,18 @@ public class TeacherController {
private ITeacherService iTeacherServ;
public TeacherController(ITeacherService iserv) {
this.iTeacherServ = iserv;
public TeacherController() {
}
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<Teacher> getAllTeacher(){
public @ResponseBody List<Teacher> getAllTeacher(){
return iTeacherServ.getAllTeacher();
}
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Teacher createTeacher( @RequestBody Teacher teach){
public @ResponseBody Teacher createTeacher( @RequestBody Teacher teach){
return teach;
}

@ -0,0 +1,17 @@
package SAE.ApiREST.WebService.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class CollectAdvice {
@ResponseBody
@ExceptionHandler(CollectException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String collectHandler(CollectException ex) {
return ex.getMessage();
}
}

@ -0,0 +1,7 @@
package SAE.ApiREST.WebService.exception;
public class CollectException extends RuntimeException {
public CollectException(String exception) {
super(exception);
}
}

@ -7,8 +7,8 @@ import java.util.List;
@Entity
public class Collect {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private final long isbn = 0;
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name = "articles")
private ArrayList<Article> articles;
@Column(name = "name")
@ -16,18 +16,24 @@ public class Collect {
@Column(name = "teacher")
private Teacher teacher;
public Collect() {
}
public Collect(String name, Teacher teacher){
this.name = name;
this.teacher = teacher;
this.articles = new ArrayList<Article>();
}
public Collect() {
public Collect(String name, Teacher teacher, Integer id){
this.name = name;
this.teacher = teacher;
this.articles = new ArrayList<Article>();
this.id = id;
}
// region Article
public long getId(){
return isbn;
public Integer getId(){
return id;
}
// endregion

@ -21,7 +21,7 @@ public class Teacher {
public Teacher(Integer id, String date, String mail, String username) {
this.id = id;
this.date = LocalDate.parse(date, DateTimeFormatter.ISO_DATE);
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.mail = mail;
this.username = username;
}

@ -6,16 +6,16 @@ import java.util.List;
public interface ICollectionService{
public List<Collect> getAllCollections();
public Collect getCollectionById(long isbn);
public Collect getCollectionById(Integer id);
public List<Collect> getAllCollectionsByName(String name);
public void deleteColletionById(long isbn);
public void deleteColletionById(Integer id);
public void deleteColletionByName(String name);
public void deleteAllColletionByName(String name);
public void addCollection(Collect collection);
public void addCollections(List<Collect> collection);
public void modifyCollectionName(Collect collection, String name);
public void modifyCollectionNameById(long isbn, String name);
public List<Article> getAllArticles(Collect collection);
public void modifyCollectionNameById(Integer id, String name);
public List<Article> getAllArticlesById(Integer id);
public void addArticle(Collect collection, Article article);
public void deleteArticle(Collect collection, Article article);
}

@ -18,13 +18,13 @@ public class StubCollectionService implements ICollectionService {
public List<Collect> getAllCollections() {
List<Collect> collections = new ArrayList<>();
collections.add(new Collect("collect1", new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque")));
collections.add(new Collect("collect1", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),0));
Collect collection2 = new Collect("collect2", new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque"));
Collect collection2 = new Collect("collect2", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),1);
collection2.addArticle(new Article("toi","azezeaea", LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
collections.add(collection2);
Collect collection3 = new Collect("collect3", new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque"));
Collect collection3 = new Collect("collect3", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),3);
collection3.addArticle(new Article("toi","azezeaea",LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
collection3.addArticle(new Article("toi","azezeaea",LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
collections.add(collection3);
@ -36,15 +36,14 @@ public class StubCollectionService implements ICollectionService {
// region GET
@Override
public Collect getCollectionById(long isbn){
public Collect getCollectionById(Integer id){
List<Collect> collections = getAllCollections();
for(Collect collection : collections){
if(collection.getId() == isbn) {
return collection;
}
}
try {
return collections.get(id);
}catch (Exception e){
return null;
}
}
@Override
public List<Collect> getAllCollectionsByName(String name){
@ -61,9 +60,9 @@ public class StubCollectionService implements ICollectionService {
// region DELETE
@Override
public void deleteColletionById(long isbn){
public void deleteColletionById(Integer id){
List<Collect> collections = getAllCollections();
Collect collection = getCollectionById(isbn);
Collect collection = getCollectionById(id);
collections.remove(collection);
}
@ -103,8 +102,8 @@ public class StubCollectionService implements ICollectionService {
}
@Override
public void modifyCollectionNameById(long isbn, String name){
Collect collection = getCollectionById(isbn);
public void modifyCollectionNameById(Integer id, String name){
Collect collection = getCollectionById(id);
modifyCollectionName(collection,name);
}
// endregion
@ -112,8 +111,13 @@ public class StubCollectionService implements ICollectionService {
// region Article
@Override
public List<Article> getAllArticles(Collect collect){
return collect.getAllArticles();
public List<Article> getAllArticlesById(Integer id){
List<Article> result = new ArrayList<>();
Collect collect = getCollectionById(id);
if (result.addAll(collect.getAllArticles())){
return result;
}
return null;
}
@Override
public void addArticle(Collect collect, Article article){

Loading…
Cancel
Save