trying to fix repository functions

feature/ProfSection
Roxane ROSSETTO 1 year ago
parent ecbe36b1ea
commit 5efc1f3057

@ -1,11 +1,10 @@
package SAE.ApiREST.WebService.controller; package SAE.ApiREST.WebService.controller;
import SAE.ApiREST.WebService.exception.TeacherAdvice;
import SAE.ApiREST.WebService.exception.TeacherException; import SAE.ApiREST.WebService.exception.TeacherException;
import SAE.ApiREST.WebService.model.Teacher; import SAE.ApiREST.WebService.model.Teacher;
import SAE.ApiREST.WebService.service.ITeacherService; import SAE.ApiREST.WebService.service.TeacherService;
import jakarta.persistence.Entity;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -13,9 +12,6 @@ import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.print.attribute.standard.Media;
import java.awt.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
@ -24,19 +20,21 @@ import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Controller @Controller
@RequestMapping("/ProfWebService") @RequestMapping("/ProfWebService")
public class TeacherController { public class TeacherController {
@Autowired @Autowired
private ITeacherService iTeacherServ; private TeacherService teacherServ;
public TeacherController(ITeacherService iserv) { public void setiTeacherServ(TeacherService iTeacherS) {
this.iTeacherServ = iserv; this.teacherServ = iTeacherS;
} }
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public @ResponseBody CollectionModel<Teacher> getAllTeacher(){ public @ResponseBody CollectionModel<Teacher> getAllTeacher(){
return CollectionModel.of( return CollectionModel.of(
iTeacherServ.getAllTeacher(), teacherServ.getAllTeacher(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withSelfRel()); linkTo(methodOn(TeacherController.class).getAllTeacher()).withSelfRel());
} }
@PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ -49,7 +47,7 @@ public class TeacherController {
} }
@GetMapping(value = "/getid/{id}") @GetMapping(value = "/getid/{id}")
public @ResponseBody EntityModel<Teacher> getTeachById(@PathVariable("id") Integer id){ public @ResponseBody EntityModel<Teacher> getTeachById(@PathVariable("id") Integer id){
Teacher tt = iTeacherServ.getTeacherById(id); Teacher tt = teacherServ.getTeacherById(id);
if( tt == null ){ if( tt == null ){
throw new TeacherException("No teacher found for this id !"); throw new TeacherException("No teacher found for this id !");
} }
@ -59,7 +57,7 @@ public class TeacherController {
} }
@GetMapping(value = "/getusername/{username}") @GetMapping(value = "/getusername/{username}")
public @ResponseBody EntityModel<Teacher> getTeachByUsername(@PathVariable("username") String username) { public @ResponseBody EntityModel<Teacher> getTeachByUsername(@PathVariable("username") String username) {
Teacher tt = iTeacherServ.getTeacherByUsername(username); Teacher tt = teacherServ.getTeacherByUsername(username);
if (tt == null) { if (tt == null) {
throw new TeacherException("No teacher found for this username"); throw new TeacherException("No teacher found for this username");
} }
@ -69,7 +67,7 @@ public class TeacherController {
} }
@GetMapping( value = "/getmail/{mail}" ) @GetMapping( value = "/getmail/{mail}" )
public @ResponseBody EntityModel<Teacher> getTeachByMail(@PathVariable("mail") String mail) { public @ResponseBody EntityModel<Teacher> getTeachByMail(@PathVariable("mail") String mail) {
Teacher tt = iTeacherServ.getTeacherByMail(mail); Teacher tt = teacherServ.getTeacherByMail(mail);
if( tt == null ) { if( tt == null ) {
throw new TeacherException("No teacher found for this mail"); throw new TeacherException("No teacher found for this mail");
} }
@ -79,7 +77,7 @@ public class TeacherController {
} }
@GetMapping( value = "/getdate/{date}" ) @GetMapping( value = "/getdate/{date}" )
public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) { public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) {
Teacher tt = iTeacherServ.getTeacherByMail(date); Teacher tt = teacherServ.getTeacherByMail(date);
if( tt == null ) { if( tt == null ) {
throw new TeacherException("No teacher found for this mail"); throw new TeacherException("No teacher found for this mail");
} }
@ -90,16 +88,15 @@ public class TeacherController {
if( username == "" ){ if( username == "" ){
throw new TeacherException("Username provided for modification is empty"); throw new TeacherException("Username provided for modification is empty");
} }
iTeacherServ.modifyUsername(tt,username); teacherServ.modifyUsername(tt,username);
return EntityModel.of(tt, return EntityModel.of(tt,
linkTo(methodOn(TeacherController.class).modifyTeachUsername(username,tt)).withSelfRel(), linkTo(methodOn(TeacherController.class).modifyTeachUsername(username,tt)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
} }
@DeleteMapping( value = "delete") @DeleteMapping( value = "delete")
public @ResponseBody EntityModel<List<Teacher>> deleteTeacher(Integer id){ public @ResponseBody EntityModel<List<Teacher>> deleteTeacher(Integer id){
return EntityModel.of(iTeacherServ.deleteTeacher(id), return EntityModel.of(teacherServ.deleteTeacher(id),
linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(), linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
} }
} }
//Todo() linkto dans service.

@ -1,9 +1,11 @@
package SAE.ApiREST.WebService.repository; package SAE.ApiREST.WebService.repository;
import SAE.ApiREST.WebService.model.Teacher; import SAE.ApiREST.WebService.model.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@Repository @Repository
@ -14,5 +16,5 @@ public interface ITeacherRepository extends JpaRepository< Teacher, Integer> {
Teacher findByMail(String mail); Teacher findByMail(String mail);
Teacher findByDate(String date); Teacher findByDate(LocalDate date);
} }

@ -7,18 +7,18 @@ import SAE.ApiREST.WebService.model.Article;
public interface IArticleService { public interface IArticleService {
// region GET // region GET
public List<Article> getAllArticles(); List<Article> getAllArticles();
Article getArticlesById(Integer id); Article getArticlesById(Integer id);
public List<Article> getArticlesByTitle(String title); List<Article> getArticlesByTitle(String title);
public List<Article> getArticlesByType(Integer type); List<Article> getArticlesByType(Integer type);
public List<Article> getVisibleArticles(); List<Article> getVisibleArticles();
public List<Article> getInvisibleArticles(); List<Article> getInvisibleArticles();
public List<Article> getArticlesAddedBefore(String dateAdded); List<Article> getArticlesAddedBefore(String dateAdded);
public List<Article> getArticlesAddedAfter(String dateAdded); List<Article> getArticlesAddedAfter(String dateAdded);
public List<Article> getArticlesAddedBetween(String beginning, String end); List<Article> getArticlesAddedBetween(String beginning, String end);
public List<Article> getArticlesPublishedBefore(String datePublished); List<Article> getArticlesPublishedBefore(String datePublished);
public List<Article> getArticlesPublishedAfter(String datePublished); List<Article> getArticlesPublishedAfter(String datePublished);
public List<Article> getArticlesPublishedBetween(String beginning, String end); List<Article> getArticlesPublishedBetween(String beginning, String end);
// endregion // endregion
} }

@ -5,17 +5,17 @@ import SAE.ApiREST.WebService.model.Article;
import java.util.List; import java.util.List;
public interface ICollectionService{ public interface ICollectionService{
public List<Collect> getAllCollections(); List<Collect> getAllCollections();
public Collect getCollectionById(long isbn); Collect getCollectionById(long isbn);
public List<Collect> getAllCollectionsByName(String name); List<Collect> getAllCollectionsByName(String name);
public void deleteColletionById(long isbn); void deleteColletionById(long isbn);
public void deleteColletionByName(String name); void deleteColletionByName(String name);
public void deleteAllColletionByName(String name); void deleteAllColletionByName(String name);
public void addCollection(Collect collection); void addCollection(Collect collection);
public void addCollections(List<Collect> collection); void addCollections(List<Collect> collection);
public void modifyCollectionName(Collect collection, String name); void modifyCollectionName(Collect collection, String name);
public void modifyCollectionNameById(long isbn, String name); void modifyCollectionNameById(long isbn, String name);
public List<Article> getAllArticles(Collect collection); List<Article> getAllArticles(Collect collection);
public void addArticle(Collect collection, Article article); void addArticle(Collect collection, Article article);
public void deleteArticle(Collect collection, Article article); void deleteArticle(Collect collection, Article article);
} }

@ -1,18 +0,0 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Teacher;
import java.time.LocalDate;
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);
}

@ -6,59 +6,64 @@ import SAE.ApiREST.WebService.repository.ITeacherRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@Service @Service
public class TeacherService implements ITeacherService { public class TeacherService {
@Autowired
private final ITeacherRepository teachRep; private final ITeacherRepository teachRep;
@Autowired
public TeacherService(ITeacherRepository teachRep) { public TeacherService(ITeacherRepository teachRep) {
this.teachRep = teachRep; this.teachRep = teachRep;
} }
@Override
public List<Teacher> getAllTeacher() { public List<Teacher> getAllTeacher() {
return teachRep.findAll(); return teachRep.findAll();
} }
@Override
public Teacher getTeacherById(Integer id) { public Teacher getTeacherById(Integer id) {
return teachRep.getReferenceById(id); return teachRep.getReferenceById(id);
} }
@Override
public Teacher getTeacherByUsername(String username) { public Teacher getTeacherByUsername(String username) {
return teachRep.findByUsername(username); return teachRep.findByUsername(username);
} }
@Override
public Teacher getTeacherByMail(String mail) { public Teacher getTeacherByMail(String mail) {
return teachRep.findByMail(mail); return teachRep.findByMail(mail);
} }
@Override
public Teacher getTeacherByDate(String date) { public Teacher getTeacherByDate(String date) {
return teachRep.findByDate(date); LocalDate d = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
return teachRep.findByDate(d);
} }
@Override
public List<Teacher> addTeacher(Teacher t) { public List<Teacher> addTeacher(Teacher t) {
List<Teacher> lT = getAllTeacher(); List<Teacher> lT = getAllTeacher();
lT.add(t); lT.add(t);
return lT; return lT;
} }
@Override
public List<Teacher> deleteTeacher(Integer id) { public List<Teacher> deleteTeacher(Integer id) {
List<Teacher> lT = getAllTeacher(); List<Teacher> lT = getAllTeacher();
lT.remove(getTeacherById(id)); lT.remove(getTeacherById(id));
return lT; return lT;
} }
@Override
public Response modifyUsername(Teacher t, String newUsername) { public Response modifyUsername(Teacher t, String newUsername) {
t.setUsername(newUsername); t.setUsername(newUsername);
return new Response(t.getId(), String.format("Username changed for {id}", t.getId())); return new Response(t.getId(), String.format("Username changed for {id}", t.getId()));

@ -1,74 +0,0 @@
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.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@Service
public class TeacherServiceStub implements ITeacherService {
//todo() recevoir collections, ajouter collections, supprimer collections
@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");
}
//Todo() Before date, After date, between date
@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