From e6c7cef921426b01c525db7772582e7e656903d1 Mon Sep 17 00:00:00 2001 From: Jannik Sinz Date: Sun, 14 Mar 2021 18:13:02 +0100 Subject: [PATCH] Feat database access repository (#15) * feat: working repository connection * feat: Add Repository and Services * feat: Add findById Co-authored-by: Gio02092001 --- .../controller/SurveyController.java | 22 +++++++++------ .../model/SurveyRepositoryGio.java | 24 ---------------- .../AnswerPossibilityRepository.java | 12 ++++++++ .../repository/AnswerRepository.java | 12 ++++++++ .../repository/ParticipantRepository.java | 12 ++++++++ .../repository/SurveyElementRepository.java | 13 +++++++++ .../repository/SurveyRepository.java | 16 +++++++++++ .../Survey_ParticipantRepository.java | 12 ++++++++ .../repository/UserRepository.java | 12 ++++++++ .../service/AnswerPossibilityService.java | 22 +++++++++++++++ .../Partycipate/service/AnswerService.java | 22 +++++++++++++++ .../Partycipate/service/DatabaseService.java | 2 +- .../service/ParticipantService.java | 22 +++++++++++++++ .../service/SurveyElementService.java | 22 +++++++++++++++ .../Partycipate/service/SurveyService.java | 21 +++++++++----- .../service/Survey_ParticipantService.java | 22 +++++++++++++++ .../Partycipate/service/UserService.java | 28 +++++++++++++++++++ 17 files changed, 256 insertions(+), 40 deletions(-) delete mode 100644 src/main/java/com/partycipate/Partycipate/model/SurveyRepositoryGio.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/AnswerPossibilityRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/AnswerRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/ParticipantRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/SurveyElementRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/SurveyRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/Survey_ParticipantRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/repository/UserRepository.java create mode 100644 src/main/java/com/partycipate/Partycipate/service/AnswerPossibilityService.java create mode 100644 src/main/java/com/partycipate/Partycipate/service/AnswerService.java create mode 100644 src/main/java/com/partycipate/Partycipate/service/ParticipantService.java create mode 100644 src/main/java/com/partycipate/Partycipate/service/SurveyElementService.java create mode 100644 src/main/java/com/partycipate/Partycipate/service/Survey_ParticipantService.java create mode 100644 src/main/java/com/partycipate/Partycipate/service/UserService.java diff --git a/src/main/java/com/partycipate/Partycipate/controller/SurveyController.java b/src/main/java/com/partycipate/Partycipate/controller/SurveyController.java index c12060c..11b49e0 100644 --- a/src/main/java/com/partycipate/Partycipate/controller/SurveyController.java +++ b/src/main/java/com/partycipate/Partycipate/controller/SurveyController.java @@ -1,27 +1,25 @@ package com.partycipate.Partycipate.controller; -import com.partycipate.Partycipate.service.ISurveyService; +import com.partycipate.Partycipate.repository.SurveyRepository; import com.partycipate.Partycipate.service.SurveyService; import com.partycipate.Partycipate.model.Survey; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.ModelAndView; import org.springframework.web.bind.annotation.*; -import java.util.*; +import java.util.Optional; @RestController @RequestMapping("/api/survey") public class SurveyController { - private final SurveyService surveyService; + @Autowired + private SurveyRepository surveyRepository; + private final SurveyService surveyService; @Autowired public SurveyController(SurveyService surveyService){ this.surveyService=surveyService; @@ -33,8 +31,16 @@ public void addSurvey(@RequestBody Survey survey){ } @GetMapping("") - public List getAllSurveys(){ + public @ResponseBody Iterable getAllSurveys(){ return surveyService.getAllSurveys(); } + @GetMapping("/{id}") + public @ResponseBody Survey getSurvey(@PathVariable("id") int id){ + return surveyService.getSurvey(id); + } + /*@GetMapping("/user/{id}") + public @ResponseBody Iterable getUserSurveys(@PathVariable("id") int id){ + return surveyService.getUserSurveys(id); + }*/ } diff --git a/src/main/java/com/partycipate/Partycipate/model/SurveyRepositoryGio.java b/src/main/java/com/partycipate/Partycipate/model/SurveyRepositoryGio.java deleted file mode 100644 index 122c85b..0000000 --- a/src/main/java/com/partycipate/Partycipate/model/SurveyRepositoryGio.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.partycipate.Partycipate.model; - -import org.hibernate.service.spi.InjectService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.CrudRepository; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface SurveyRepositoryGio extends CrudRepository { - - //@Query(value = "select * from survey Where id = 1", nativeQuery = true) - //Survey findSurveyById(int id); - - - //@Query(value ="Insert Into survey VALUES (?,?,?,?,?,?)", nativeQuery = true) - //String createSurvey(int id, String cookie, String creation_date, String title, int user_id); - - //List findSurveyByUserId (Long user_id); -} - diff --git a/src/main/java/com/partycipate/Partycipate/repository/AnswerPossibilityRepository.java b/src/main/java/com/partycipate/Partycipate/repository/AnswerPossibilityRepository.java new file mode 100644 index 0000000..984b184 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/AnswerPossibilityRepository.java @@ -0,0 +1,12 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AnswerPossibilityRepository extends CrudRepository { + +} + diff --git a/src/main/java/com/partycipate/Partycipate/repository/AnswerRepository.java b/src/main/java/com/partycipate/Partycipate/repository/AnswerRepository.java new file mode 100644 index 0000000..b3c448c --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/AnswerRepository.java @@ -0,0 +1,12 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AnswerRepository extends CrudRepository { + +} + diff --git a/src/main/java/com/partycipate/Partycipate/repository/ParticipantRepository.java b/src/main/java/com/partycipate/Partycipate/repository/ParticipantRepository.java new file mode 100644 index 0000000..d570ff3 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/ParticipantRepository.java @@ -0,0 +1,12 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ParticipantRepository extends CrudRepository { + +} + diff --git a/src/main/java/com/partycipate/Partycipate/repository/SurveyElementRepository.java b/src/main/java/com/partycipate/Partycipate/repository/SurveyElementRepository.java new file mode 100644 index 0000000..f35b3c8 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/SurveyElementRepository.java @@ -0,0 +1,13 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface SurveyElementRepository extends CrudRepository { + + +} + diff --git a/src/main/java/com/partycipate/Partycipate/repository/SurveyRepository.java b/src/main/java/com/partycipate/Partycipate/repository/SurveyRepository.java new file mode 100644 index 0000000..26caa1c --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/SurveyRepository.java @@ -0,0 +1,16 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; + +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface SurveyRepository extends CrudRepository { + Survey findById(int id); + //Survey findAllByUser_id(int id); +} + diff --git a/src/main/java/com/partycipate/Partycipate/repository/Survey_ParticipantRepository.java b/src/main/java/com/partycipate/Partycipate/repository/Survey_ParticipantRepository.java new file mode 100644 index 0000000..1dd92bb --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/Survey_ParticipantRepository.java @@ -0,0 +1,12 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface Survey_ParticipantRepository extends CrudRepository { + +} + diff --git a/src/main/java/com/partycipate/Partycipate/repository/UserRepository.java b/src/main/java/com/partycipate/Partycipate/repository/UserRepository.java new file mode 100644 index 0000000..51baa53 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/repository/UserRepository.java @@ -0,0 +1,12 @@ +package com.partycipate.Partycipate.repository; + + +import com.partycipate.Partycipate.model.Survey; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserRepository extends CrudRepository { + +} + diff --git a/src/main/java/com/partycipate/Partycipate/service/AnswerPossibilityService.java b/src/main/java/com/partycipate/Partycipate/service/AnswerPossibilityService.java new file mode 100644 index 0000000..9c11a4a --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/service/AnswerPossibilityService.java @@ -0,0 +1,22 @@ +package com.partycipate.Partycipate.service; + +import com.partycipate.Partycipate.dao.SurveyDao; +import com.partycipate.Partycipate.repository.SurveyRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class AnswerPossibilityService { + + private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; + + @Autowired + public AnswerPossibilityService(@Qualifier("fakeDao") SurveyDao surveyDao) { + this.surveyDao = surveyDao; + } + + +} diff --git a/src/main/java/com/partycipate/Partycipate/service/AnswerService.java b/src/main/java/com/partycipate/Partycipate/service/AnswerService.java new file mode 100644 index 0000000..9784f9f --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/service/AnswerService.java @@ -0,0 +1,22 @@ +package com.partycipate.Partycipate.service; + +import com.partycipate.Partycipate.dao.SurveyDao; +import com.partycipate.Partycipate.repository.SurveyRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class AnswerService { + + private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; + + @Autowired + public AnswerService(@Qualifier("fakeDao") SurveyDao surveyDao) { + this.surveyDao = surveyDao; + } + + +} diff --git a/src/main/java/com/partycipate/Partycipate/service/DatabaseService.java b/src/main/java/com/partycipate/Partycipate/service/DatabaseService.java index 46bd651..57b5430 100644 --- a/src/main/java/com/partycipate/Partycipate/service/DatabaseService.java +++ b/src/main/java/com/partycipate/Partycipate/service/DatabaseService.java @@ -3,7 +3,7 @@ import com.partycipate.Partycipate.model.Survey; -import com.partycipate.models.User; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; diff --git a/src/main/java/com/partycipate/Partycipate/service/ParticipantService.java b/src/main/java/com/partycipate/Partycipate/service/ParticipantService.java new file mode 100644 index 0000000..59447ef --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/service/ParticipantService.java @@ -0,0 +1,22 @@ +package com.partycipate.Partycipate.service; + +import com.partycipate.Partycipate.dao.SurveyDao; +import com.partycipate.Partycipate.repository.SurveyRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class ParticipantService { + + private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; + + @Autowired + public ParticipantService(@Qualifier("fakeDao") SurveyDao surveyDao) { + this.surveyDao = surveyDao; + } + + +} diff --git a/src/main/java/com/partycipate/Partycipate/service/SurveyElementService.java b/src/main/java/com/partycipate/Partycipate/service/SurveyElementService.java new file mode 100644 index 0000000..e06e121 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/service/SurveyElementService.java @@ -0,0 +1,22 @@ +package com.partycipate.Partycipate.service; + +import com.partycipate.Partycipate.dao.SurveyDao; +import com.partycipate.Partycipate.repository.SurveyRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class SurveyElementService { + + private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; + + @Autowired + public SurveyElementService(@Qualifier("fakeDao") SurveyDao surveyDao) { + this.surveyDao = surveyDao; + } + + +} diff --git a/src/main/java/com/partycipate/Partycipate/service/SurveyService.java b/src/main/java/com/partycipate/Partycipate/service/SurveyService.java index d6b40a7..2563569 100644 --- a/src/main/java/com/partycipate/Partycipate/service/SurveyService.java +++ b/src/main/java/com/partycipate/Partycipate/service/SurveyService.java @@ -2,18 +2,22 @@ import com.partycipate.Partycipate.dao.SurveyDao; import com.partycipate.Partycipate.model.Survey; +import com.partycipate.Partycipate.repository.SurveyRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; import java.util.Optional; @Service -public class SurveyService implements ISurveyService{ +public class SurveyService { private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; @Autowired public SurveyService(@Qualifier("fakeDao") SurveyDao surveyDao) { @@ -25,8 +29,8 @@ public Survey addSurvey(Survey survey){ } @GetMapping() - public List getAllSurveys(){ - return surveyDao.selectAllSurveys(); + public @ResponseBody Iterable getAllSurveys(){ + return surveyRepository.findAll(); } @@ -36,13 +40,16 @@ public static Survey getRandomSurvey(int id) { } - @Override + public List findAll() { return null; } - @Override - public Optional getSurvey(Long id) { - return Optional.empty(); + + public Survey getSurvey(int id) { + return surveyRepository.findById(id); } + /*public Iterable getUserSurveys(int id){ + return (Iterable) surveyRepository.findAllByUser_id(id); + }*/ } diff --git a/src/main/java/com/partycipate/Partycipate/service/Survey_ParticipantService.java b/src/main/java/com/partycipate/Partycipate/service/Survey_ParticipantService.java new file mode 100644 index 0000000..cea7713 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/service/Survey_ParticipantService.java @@ -0,0 +1,22 @@ +package com.partycipate.Partycipate.service; + +import com.partycipate.Partycipate.dao.SurveyDao; +import com.partycipate.Partycipate.repository.SurveyRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class Survey_ParticipantService { + + private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; + + @Autowired + public Survey_ParticipantService(@Qualifier("fakeDao") SurveyDao surveyDao) { + this.surveyDao = surveyDao; + } + + +} diff --git a/src/main/java/com/partycipate/Partycipate/service/UserService.java b/src/main/java/com/partycipate/Partycipate/service/UserService.java new file mode 100644 index 0000000..ce233a7 --- /dev/null +++ b/src/main/java/com/partycipate/Partycipate/service/UserService.java @@ -0,0 +1,28 @@ +package com.partycipate.Partycipate.service; + +import com.partycipate.Partycipate.dao.SurveyDao; +import com.partycipate.Partycipate.model.Survey; +import com.partycipate.Partycipate.repository.SurveyRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.List; +import java.util.Optional; + +@Service +public class UserService { + + private final SurveyDao surveyDao; + @Autowired + private SurveyRepository surveyRepository; + + @Autowired + public UserService(@Qualifier("fakeDao") SurveyDao surveyDao) { + this.surveyDao = surveyDao; + } + + +}