Add init folder structure and add some logic for document and user

This commit is contained in:
Mika Bomm 2024-09-01 20:50:58 +02:00
parent c121d62fb4
commit 23affca287
5 changed files with 194 additions and 19 deletions

View file

@ -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<User> 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) {}
}

View file

@ -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<Document> getAllDocuments() {
return documentService.findAll();
}
@GetMapping("/{id}")
public ResponseEntity<Document> getDocumentById(@PathVariable UUID id) {
Optional<Document> 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<Document> updateDocument(@PathVariable UUID id, @RequestBody Document documentDetails) {
Optional<Document> 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<Void> deleteDocument(@PathVariable UUID id) {
documentService.deleteById(id);
return ResponseEntity.noContent().build();
}
}

View file

@ -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<User> getAllUsers() {
return userService.findAll();
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable UUID id) {
Optional<User> 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<User> updateUser(@PathVariable UUID id, @RequestBody User userDetails) {
Optional<User> 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<Void> deleteUser(@PathVariable UUID id) {
userService.deleteById(id);
return ResponseEntity.noContent().build();
}
}

View file

@ -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<Document> findAll() {
return documentRepository.findAll();
}
public Optional<Document> findById(UUID id) {
return documentRepository.findById(id);
}
public Document save(Document document) {
return documentRepository.save(document);
}
public void deleteById(UUID id) {
documentRepository.deleteById(id);
}
}

View file

@ -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<User> findAll() {
return userRepository.findAll();
}
public Optional<User> findById(UUID id) {
return userRepository.findById(id);
}
public User save(User user) {
return userRepository.save(user);
}
public void deleteById(UUID id) {
userRepository.deleteById(id);
}
}