From 23affca2874c4459d8d2d1c779ca18e86968a3de Mon Sep 17 00:00:00 2001 From: Mika Bomm Date: Sun, 1 Sep 2024 20:50:58 +0200 Subject: [PATCH] Add init folder structure and add some logic for document and user --- .../docusphere/DocuSphereApplication.java | 33 +++++------ .../controller/DocumentController.java | 56 ++++++++++++++++++ .../docusphere/controller/UserController.java | 58 +++++++++++++++++++ .../docusphere/service/DocumentService.java | 33 +++++++++++ .../mixel/docusphere/service/UserService.java | 33 +++++++++++ 5 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 server/src/main/java/com/mixel/docusphere/controller/DocumentController.java create mode 100644 server/src/main/java/com/mixel/docusphere/controller/UserController.java create mode 100644 server/src/main/java/com/mixel/docusphere/service/DocumentService.java create mode 100644 server/src/main/java/com/mixel/docusphere/service/UserService.java diff --git a/server/src/main/java/com/mixel/docusphere/DocuSphereApplication.java b/server/src/main/java/com/mixel/docusphere/DocuSphereApplication.java index 018381f..c79016b 100644 --- a/server/src/main/java/com/mixel/docusphere/DocuSphereApplication.java +++ b/server/src/main/java/com/mixel/docusphere/DocuSphereApplication.java @@ -1,36 +1,31 @@ package com.mixel.docusphere; +import com.mixel.docusphere.entity.User; +import com.mixel.docusphere.repository.UserRepository; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @SpringBootApplication @RestController public class DocuSphereApplication { + public DocuSphereApplication(UserRepository userRepository) { + this.userRepository = userRepository; + } + public static void main(String[] args) { SpringApplication.run(DocuSphereApplication.class, args); } - @GetMapping("/hello") - public String helloWorld() { - return "Hello World!"; + private final UserRepository userRepository; + + @GetMapping("/api/v1/users") + public List getUsers() { + return userRepository.findAll(); } - - @GetMapping("/") - private SomeImportantData index() { - return new SomeImportantData( - "Important keep secret", - new Person("SomeObscure Person", 96, - new FavLanguage("Java", true, 'J') //Although the compiled status is a lie i mean kind of - ) - ); - } - - record SomeImportantData(String name, Person person) {} - - record Person(String name, int age, FavLanguage favLanguage) {} - - record FavLanguage(String name, Boolean compiled, Character FirstCharacter) {} } diff --git a/server/src/main/java/com/mixel/docusphere/controller/DocumentController.java b/server/src/main/java/com/mixel/docusphere/controller/DocumentController.java new file mode 100644 index 0000000..c3f0ac6 --- /dev/null +++ b/server/src/main/java/com/mixel/docusphere/controller/DocumentController.java @@ -0,0 +1,56 @@ +package com.mixel.docusphere.controller; + +import com.mixel.docusphere.entity.Document; +import com.mixel.docusphere.service.DocumentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@RestController +@RequestMapping("/documents") +public class DocumentController { + + @Autowired + private DocumentService documentService; + + @GetMapping + public List getAllDocuments() { + return documentService.findAll(); + } + + @GetMapping("/{id}") + public ResponseEntity getDocumentById(@PathVariable UUID id) { + Optional document = documentService.findById(id); + return document.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + } + + @PostMapping + public Document createDocument(@RequestBody Document document) { + return documentService.save(document); + } + + @PutMapping("/{id}") + public ResponseEntity updateDocument(@PathVariable UUID id, @RequestBody Document documentDetails) { + Optional document = documentService.findById(id); + if (document.isPresent()) { + Document updatedDocument = document.get(); + updatedDocument.setName(documentDetails.getName()); + updatedDocument.setS3Path(documentDetails.getS3Path()); + updatedDocument.setUser(documentDetails.getUser()); + documentService.save(updatedDocument); + return ResponseEntity.ok(updatedDocument); + } else { + return ResponseEntity.notFound().build(); + } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteDocument(@PathVariable UUID id) { + documentService.deleteById(id); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/server/src/main/java/com/mixel/docusphere/controller/UserController.java b/server/src/main/java/com/mixel/docusphere/controller/UserController.java new file mode 100644 index 0000000..211c8b7 --- /dev/null +++ b/server/src/main/java/com/mixel/docusphere/controller/UserController.java @@ -0,0 +1,58 @@ +package com.mixel.docusphere.controller; + +import com.mixel.docusphere.entity.User; +import com.mixel.docusphere.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@RestController +@RequestMapping("/users") +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping + public List getAllUsers() { + return userService.findAll(); + } + + @GetMapping("/{id}") + public ResponseEntity getUserById(@PathVariable UUID id) { + Optional user = userService.findById(id); + return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + } + + @PostMapping + public User createUser(@RequestBody User user) { + return userService.save(user); + } + + @PutMapping("/{id}") + public ResponseEntity updateUser(@PathVariable UUID id, @RequestBody User userDetails) { + Optional user = userService.findById(id); + if (user.isPresent()) { + User updatedUser = user.get(); + updatedUser.setUsername(userDetails.getUsername()); + updatedUser.setName(userDetails.getName()); + updatedUser.setEmail(userDetails.getEmail()); + updatedUser.setPasswordHash(userDetails.getPasswordHash()); + updatedUser.setPasswordSalt(userDetails.getPasswordSalt()); + userService.save(updatedUser); + return ResponseEntity.ok(updatedUser); + } else { + return ResponseEntity.notFound().build(); + } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteUser(@PathVariable UUID id) { + userService.deleteById(id); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/server/src/main/java/com/mixel/docusphere/service/DocumentService.java b/server/src/main/java/com/mixel/docusphere/service/DocumentService.java new file mode 100644 index 0000000..6ae89eb --- /dev/null +++ b/server/src/main/java/com/mixel/docusphere/service/DocumentService.java @@ -0,0 +1,33 @@ +package com.mixel.docusphere.service; + +import com.mixel.docusphere.entity.Document; +import com.mixel.docusphere.repository.DocumentRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Service +public class DocumentService { + + @Autowired + private DocumentRepository documentRepository; + + public List findAll() { + return documentRepository.findAll(); + } + + public Optional findById(UUID id) { + return documentRepository.findById(id); + } + + public Document save(Document document) { + return documentRepository.save(document); + } + + public void deleteById(UUID id) { + documentRepository.deleteById(id); + } +} \ No newline at end of file diff --git a/server/src/main/java/com/mixel/docusphere/service/UserService.java b/server/src/main/java/com/mixel/docusphere/service/UserService.java new file mode 100644 index 0000000..4a9845a --- /dev/null +++ b/server/src/main/java/com/mixel/docusphere/service/UserService.java @@ -0,0 +1,33 @@ +package com.mixel.docusphere.service; + +import com.mixel.docusphere.entity.User; +import com.mixel.docusphere.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Service +public class UserService { + + @Autowired + private UserRepository userRepository; + + public List findAll() { + return userRepository.findAll(); + } + + public Optional findById(UUID id) { + return userRepository.findById(id); + } + + public User save(User user) { + return userRepository.save(user); + } + + public void deleteById(UUID id) { + userRepository.deleteById(id); + } +} \ No newline at end of file