Compare commits

...

4 commits

9 changed files with 248 additions and 22 deletions

View file

@ -2,7 +2,6 @@ package com.mixel.docusphere;
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;
@SpringBootApplication
@ -12,25 +11,4 @@ public class DocuSphereApplication {
public static void main(String[] args) {
SpringApplication.run(DocuSphereApplication.class, args);
}
@GetMapping("/hello")
public String helloWorld() {
return "Hello World!";
}
@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,60 @@
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("/api/v1/documents")
public class DocumentController {
private final DocumentService documentService;
@Autowired
public DocumentController(DocumentService documentService) {
this.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,69 @@
package com.mixel.docusphere.controller;
import com.mixel.docusphere.dto.UserDTO;
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("/api/v1/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.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 UserDTO userDTO) {
User user = new User();
user.setUsername(userDTO.getUsername());
user.setName(userDTO.getName());
user.setEmail(userDTO.getEmail());
user.setPasswordHash(userDTO.getPassword());
return userService.save(user);
}
// FIXME: Use DTO instead of entity
@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,6 @@
package com.mixel.docusphere.dto;
// FIXME: Implement document DTO
public class DocumentDTO {
}

View file

@ -0,0 +1,41 @@
package com.mixel.docusphere.dto;
public class UserDTO {
private String username;
private String name;
private String email;
private String password;
// Getters and Setters
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View file

@ -15,6 +15,9 @@ public class Document {
@Column(name = "Name", nullable = false)
private String name;
@Column(name = "Description", nullable = true)
private String description;
@Column(name = "S3Path", nullable = false)
private String s3Path;

View file

@ -1,5 +1,6 @@
package com.mixel.docusphere.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import java.util.UUID;
@ -21,9 +22,11 @@ public class User {
@Column(name = "Email", nullable = false, unique = true)
private String email;
@JsonIgnore
@Column(name = "PasswordHash", nullable = false)
private String passwordHash;
@JsonIgnore
@Column(name = "PasswordSalt", nullable = false)
private String passwordSalt;

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);
}
}