manque tests

Maxime
Maxime POINT 1 year ago
parent c4f977c45a
commit 808ccbc480

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

@ -19,19 +19,18 @@ public class TeacherController {
private ITeacherService iTeacherServ; private ITeacherService iTeacherServ;
public TeacherController(ITeacherService iserv) { public TeacherController() {
this.iTeacherServ = iserv;
} }
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public List<Teacher> getAllTeacher(){ public @ResponseBody List<Teacher> getAllTeacher(){
return iTeacherServ.getAllTeacher(); return iTeacherServ.getAllTeacher();
} }
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public Teacher createTeacher( @RequestBody Teacher teach){ public @ResponseBody Teacher createTeacher( @RequestBody Teacher teach){
return 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 @Entity
public class Collect { public class Collect {
@Id @Id
@GeneratedValue(strategy= GenerationType.AUTO) @GeneratedValue(strategy=GenerationType.AUTO)
private final long isbn = 0; private Integer id;
@Column(name = "articles") @Column(name = "articles")
private ArrayList<Article> articles; private ArrayList<Article> articles;
@Column(name = "name") @Column(name = "name")
@ -16,18 +16,24 @@ public class Collect {
@Column(name = "teacher") @Column(name = "teacher")
private Teacher teacher; private Teacher teacher;
public Collect() {
}
public Collect(String name, Teacher teacher){ public Collect(String name, Teacher teacher){
this.name = name; this.name = name;
this.teacher = teacher; this.teacher = teacher;
this.articles = new ArrayList<Article>(); this.articles = new ArrayList<Article>();
} }
public Collect(String name, Teacher teacher, Integer id){
public Collect() { this.name = name;
this.teacher = teacher;
this.articles = new ArrayList<Article>();
this.id = id;
} }
// region Article // region Article
public long getId(){ public Integer getId(){
return isbn; return id;
} }
// endregion // endregion

@ -21,7 +21,7 @@ public class Teacher {
public Teacher(Integer id, String date, String mail, String username) { public Teacher(Integer id, String date, String mail, String username) {
this.id = id; 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.mail = mail;
this.username = username; this.username = username;
} }

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

@ -18,13 +18,13 @@ public class StubCollectionService implements ICollectionService {
public List<Collect> getAllCollections() { public List<Collect> getAllCollections() {
List<Collect> collections = new ArrayList<>(); 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)); collection2.addArticle(new Article("toi","azezeaea", LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
collections.add(collection2); 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));
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); collections.add(collection3);
@ -36,14 +36,13 @@ public class StubCollectionService implements ICollectionService {
// region GET // region GET
@Override @Override
public Collect getCollectionById(long isbn){ public Collect getCollectionById(Integer id){
List<Collect> collections = getAllCollections(); List<Collect> collections = getAllCollections();
for(Collect collection : collections){ try {
if(collection.getId() == isbn) { return collections.get(id);
return collection; }catch (Exception e){
} return null;
} }
return null;
} }
@Override @Override
@ -61,9 +60,9 @@ public class StubCollectionService implements ICollectionService {
// region DELETE // region DELETE
@Override @Override
public void deleteColletionById(long isbn){ public void deleteColletionById(Integer id){
List<Collect> collections = getAllCollections(); List<Collect> collections = getAllCollections();
Collect collection = getCollectionById(isbn); Collect collection = getCollectionById(id);
collections.remove(collection); collections.remove(collection);
} }
@ -103,8 +102,8 @@ public class StubCollectionService implements ICollectionService {
} }
@Override @Override
public void modifyCollectionNameById(long isbn, String name){ public void modifyCollectionNameById(Integer id, String name){
Collect collection = getCollectionById(isbn); Collect collection = getCollectionById(id);
modifyCollectionName(collection,name); modifyCollectionName(collection,name);
} }
// endregion // endregion
@ -112,8 +111,13 @@ public class StubCollectionService implements ICollectionService {
// region Article // region Article
@Override @Override
public List<Article> getAllArticles(Collect collect){ public List<Article> getAllArticlesById(Integer id){
return collect.getAllArticles(); List<Article> result = new ArrayList<>();
Collect collect = getCollectionById(id);
if (result.addAll(collect.getAllArticles())){
return result;
}
return null;
} }
@Override @Override
public void addArticle(Collect collect, Article article){ public void addArticle(Collect collect, Article article){

Loading…
Cancel
Save