merge Article + clean code

Maxime^2
felix 1 year ago
commit c526cfd0ac

@ -0,0 +1,11 @@
package SAE.ApiREST.WebService.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/CollectWebService")
public class CollectController {
}

@ -0,0 +1,99 @@
package SAE.ApiREST.WebService.controller;
import SAE.ApiREST.WebService.exception.TeacherException;
import SAE.ApiREST.WebService.model.Teacher;
import SAE.ApiREST.WebService.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Controller
@RequestMapping("/ProfWebService")
public class TeacherController {
@Autowired
private ITeacherService iTeacherServ;
public TeacherController(ITeacherService iserv) {
this.iTeacherServ = iserv;
}
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody CollectionModel<Teacher> getAllTeacher(){
return CollectionModel.of(
iTeacherServ.getAllTeacher(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withSelfRel());
}
@PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody EntityModel<Teacher> createTeacher( @RequestBody Teacher teach){
return EntityModel.of(teach,
linkTo(methodOn(TeacherController.class).createTeacher(teach)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
@GetMapping(value = "/getid/{id}")
public @ResponseBody EntityModel<Teacher> getTeachById(@PathVariable("id") Integer id){
Teacher tt = iTeacherServ.getTeacherById(id);
if( tt == null ){
throw new TeacherException("No teacher found for this id !");
}
return EntityModel.of(tt,
linkTo(methodOn(TeacherController.class).getTeachById(id)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
@GetMapping(value = "/getusername/{username}")
public @ResponseBody EntityModel<Teacher> getTeachByUsername(@PathVariable("username") String username) {
Teacher tt = iTeacherServ.getTeacherByUsername(username);
if (tt == null) {
throw new TeacherException("No teacher found for this username");
}
return EntityModel.of(tt,
linkTo(methodOn(TeacherController.class).getTeachByUsername(username)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
@GetMapping( value = "/getmail/{mail}" )
public @ResponseBody EntityModel<Teacher> getTeachByMail(@PathVariable("mail") String mail) {
Teacher tt = iTeacherServ.getTeacherByMail(mail);
if( tt == null ) {
throw new TeacherException("No teacher found for this mail");
}
return EntityModel.of(tt,
linkTo(methodOn(TeacherController.class).getTeachByMail(mail)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
@GetMapping( value = "/getdate/{date}" )
public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) {
Teacher tt = iTeacherServ.getTeacherByMail(date);
if( tt == null ) {
throw new TeacherException("No teacher found for this mail");
}
return tt;
}
@PutMapping( value = "/modify/{username}" )
public @ResponseBody EntityModel<Teacher> modifyTeachUsername(@PathVariable("username") String username, Teacher tt){
if( username == "" ){
throw new TeacherException("Username provided for modification is empty");
}
iTeacherServ.modifyUsername(tt,username);
return EntityModel.of(tt,
linkTo(methodOn(TeacherController.class).modifyTeachUsername(username,tt)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
@DeleteMapping( value = "delete")
public @ResponseBody EntityModel<List<Teacher>> deleteTeacher(Integer id){
return EntityModel.of(iTeacherServ.deleteTeacher(id),
linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
}

@ -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 TeacherAdvice {
@ResponseBody
@ExceptionHandler(TeacherException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String teacherNFHandler( TeacherException e) {
return e.getMessage();
}
}

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

@ -0,0 +1,70 @@
package SAE.ApiREST.WebService.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Collect {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private final long isbn = 0;
@Column(name = "articles")
private ArrayList<Article> articles;
@Column(name = "name")
private String name;
@Column(name = "teacher")
private Teacher teacher;
public Collect(String name, Teacher teacher){
this.name = name;
this.teacher = teacher;
this.articles = new ArrayList<Article>();
}
public Collect() {
}
// region Article
public long getId(){
return isbn;
}
// endregion
// region Article
public List<Article> getAllArticles(){
return articles;
}
// region addArticle
public void addArticle(Article article){
if(!this.articles.contains(article)){
this.articles.add(article);
}
}
public void addArticles(List<Article> articles){
for(Article article : articles){
addArticle(article);
}
}
// endregion
// region removeArticle
public void removeArticle(Article article){
this.articles.remove(article);
}
public void removeArticles(List<Article> articles){
this.articles.removeAll(articles);
}
// endregion
// endregion
// region name
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
// endregion
}

@ -0,0 +1,59 @@
package SAE.ApiREST.WebService.model;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Entity
public class Teacher {
@Id
private Integer id;
private LocalDate date;
private String mail;
private String username;
public Teacher() {
}
public Teacher(Integer id, String date, String mail, String username) {
this.id = id;
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.mail = mail;
this.username = username;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDate getDate() {
return date;
}
public void setDate(String date) {
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

@ -0,0 +1,21 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.model.Collect;
import SAE.ApiREST.WebService.model.Article;
import java.util.List;
public interface ICollectionService{
public List<Collect> getAllCollections();
public Collect getCollectionById(long isbn);
public List<Collect> getAllCollectionsByName(String name);
public void deleteColletionById(long isbn);
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 addArticle(Collect collection, Article article);
public void deleteArticle(Collect collection, Article article);
}

@ -0,0 +1,17 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Teacher;
import java.util.List;
public interface ITeacherService {
public List<Teacher> getAllTeacher();
public Teacher getTeacherById(Integer id);
public Teacher getTeacherByUsername(String username);
public Teacher getTeacherByMail(String mail);
public Teacher getTeacherByDate(String date);
public List<Teacher> addTeacher(Teacher t);
public List<Teacher> deleteTeacher(Integer id);
public Response modifyUsername(Teacher t, String newUsername);
}

@ -0,0 +1,89 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.model.Collect;
import java.util.ArrayList;
import java.util.List;
import SAE.ApiREST.WebService.model.Article;
import org.springframework.stereotype.Service;
@Service
public class StubCollectionService implements ICollectionService {
private ArrayList<Collect> collections;
public List<Collect> getAllCollections() {
return this.collections;
}
// region Collection
// region GET
public Collect getCollectionById(long isbn){
for(Collect collection : this.collections){
if(collection.getId() == isbn) {
return collection;
}
}
return null;
}
public List<Collect> getAllCollectionsByName(String name){
ArrayList<Collect> repCollections = new ArrayList<Collect>();
for(Collect collection : this.collections){
if(collection.getName() == name) {
repCollections.add(collection);
}
}
return repCollections;
}
// endregion
// region DELETE
public void deleteColletionById(long isbn){
Collect collection = getCollectionById(isbn);
this.collections.remove(collection);
}
public void deleteColletionByName(String name){
List<Collect> collectionsByName = getAllCollectionsByName(name);
this.collections.remove(collectionsByName.get(0));
}
public void deleteAllColletionByName(String name){
List<Collect> collectionsByName = getAllCollectionsByName(name);
this.collections.removeAll(collectionsByName);
}
@Override
public void addCollection(Collect collection) {
this.collections.add(collection);
}
// endregion
// region PUT
public void addCollections(List<Collect> collections){
this.collections.addAll(collections);
}
// endregion
// region POST
public void modifyCollectionName(Collect collection, String name){
collection.setName(name);
}
public void modifyCollectionNameById(long isbn, String name){
Collect collection = getCollectionById(isbn);
modifyCollectionName(collection,name);
}
// endregion
// endregion
// region Article
public List<Article> getAllArticles(Collect collect){
return collect.getAllArticles();
}
public void addArticle(Collect collect, Article article){
collect.addArticle(article);
}
public void deleteArticle(Collect collect, Article article){
collect.removeArticle(article);
}
// endregion
}

@ -0,0 +1,69 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.exception.TeacherException;
import SAE.ApiREST.WebService.model.Teacher;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class TeacherServiceStub implements ITeacherService {
@Override
public List<Teacher> getAllTeacher() {
List<Teacher> allTeacher = new ArrayList<Teacher>();
allTeacher.add(new Teacher(1, "12-01-2023", "aline.alipres@gmail.com", "MsGarconManque"));
allTeacher.add(new Teacher(2, "20-08-2023", "Viviane.Delvecchio@gmail.com", "MmeMath"));
return allTeacher;
}
@Override
public Teacher getTeacherById(Integer id) {
return new Teacher(id, "10-01-2021", "exemple.gmail.com", "testest");
}
@Override
public Teacher getTeacherByUsername(String username) { return new Teacher(12, "30-08-2020", "dadadou@gmail.com", username); }
@Override
public Teacher getTeacherByMail(String mail) {
return new Teacher(20, "24-12-2021", mail, "tructruc");
}
@Override
public Teacher getTeacherByDate(String date) {
return new Teacher(5, date, "doudouda@gmail.com", "username");
}
@Override
public List<Teacher> addTeacher(Teacher t) {
List<Teacher> lteach = new ArrayList<Teacher>();
lteach.add(t);
return lteach;
}
@Override
public List<Teacher> deleteTeacher(Integer id) {
List<Teacher> allTeacher = new ArrayList<Teacher>();
allTeacher.add(new Teacher(1,"12-01-2023", "aline.alipres@gmail.com", "MsGarconManque"));
allTeacher.add(new Teacher(2, "20-08-2023", "Viviane.Delvecchio@gmail.com", "MmeMath"));
if(allTeacher.remove(getTeacherById(id))){
return allTeacher;
} else {
throw new TeacherException(String.format("Teacher {id} isn't removed", id));
}
}
public Response modifyUsername(Teacher t, String newUsername){
t.setUsername(newUsername);
return new Response(t.getId(),String.format("This user %s has changed username", t.getMail()));
}
}
Loading…
Cancel
Save