Skip to content
Snippets Groups Projects
Commit 02ac9eb3 authored by Tini Tran's avatar Tini Tran
Browse files

Merge branch 'main' into 'rest-endpoints'

# Conflicts:
#   src/main/java/ntnu/idatt2105/group44/trivioServer/controller/UserController.java
#   src/main/java/ntnu/idatt2105/group44/trivioServer/service/UserService.java
parents f9716d4c 5b1bb307
No related branches found
No related tags found
1 merge request!1added endpoints to backend
Showing
with 232 additions and 9 deletions
package ntnu.idatt2105.group44.trivioServer.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
}
package ntnu.idatt2105.group44.trivioServer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ntnu.idatt2105.group44.trivioServer.model.Message;
import ntnu.idatt2105.group44.trivioServer.model.UserResponse;
import ntnu.idatt2105.group44.trivioServer.service.MessageService;
import java.util.List;
@RestController
@RequestMapping("/messages")
public class MessageController {
private final MessageService messageService;
@Autowired
public MessageController(MessageService messageService) {
this.messageService = messageService;
}
@PostMapping
public ResponseEntity<UserResponse> createMessage(@RequestBody Message message) {
try {
messageService.createMessage(message);
return ResponseEntity.ok(new UserResponse("Message sent. Thanks for feedback!"));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new UserResponse("Failed to send message. Please try again later."));
}
}
@GetMapping
public ResponseEntity<List<Message>> getAllMessages() {
List<Message> messages = messageService.getAllMessages();
return ResponseEntity.ok(messages);
}
}
package ntnu.idatt2105.group44.trivioServer.controller;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
import ntnu.idatt2105.group44.trivioServer.model.User;
import ntnu.idatt2105.group44.trivioServer.model.UserResponse;
import ntnu.idatt2105.group44.trivioServer.model.LoginRequest;
import ntnu.idatt2105.group44.trivioServer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -28,7 +30,6 @@ public class UserController {
this.userService = userService;
}
@GetMapping
public List<User> getUsers() {
return userService.getAllUsers();
......@@ -39,10 +40,26 @@ public class UserController {
return userService.getUserById(userId);
}
@PostMapping("/create")
public ResponseEntity<UserResponse> createUser(@RequestBody User user) {
/*
* User existingUser = userService.getUserByUsername(user.getUsername());
* if (existingUser != null) {
* return ResponseEntity.badRequest().body(new
* UserResponse("Username already exists"));
* }
*/
userService.createUser(user.getUsername(), user.getPassword(), user.getEmail());
return ResponseEntity.ok(new UserResponse("User created"));
}
@PostMapping
public ResponseEntity<String> addUser(@RequestBody User user){
userService.addUser(user);
return new ResponseEntity<>("created!", HttpStatus.CREATED);
@PostMapping("/login")
public ResponseEntity<UserResponse> loginUser(@RequestBody LoginRequest loginRequest) {
boolean isValid = userService.validateUser(loginRequest.getUsername(), loginRequest.getPassword());
if (isValid) {
return ResponseEntity.ok(new UserResponse("Login Successful"));
} else {
return ResponseEntity.badRequest().body(new UserResponse("Invalid username or password"));
}
}
}
package ntnu.idatt2105.group44.trivioServer.model;
public class LoginRequest {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package ntnu.idatt2105.group44.trivioServer.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
@Entity
@Table(name = "messages")
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String senderName;
private String senderEmail;
private String content;
public Message() {
}
public Message(String senderName, String senderEmail, String content) {
this.senderName = senderName;
this.senderEmail = senderEmail;
this.content = content;
}
public Long getId() {
return id;
}
public String getSenderName() {
return senderName;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
public String getSenderEmail() {
return senderEmail;
}
public void setSenderEmail(String senderEmail) {
this.senderEmail = senderEmail;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
......@@ -26,6 +26,13 @@ public class User {
public User(){
}
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public Long getId() {
return id;
}
......
package ntnu.idatt2105.group44.trivioServer.model;
public class UserResponse {
private String message;
public UserResponse(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
\ No newline at end of file
package ntnu.idatt2105.group44.trivioServer.repository;
import ntnu.idatt2105.group44.trivioServer.model.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface MessageRepository extends JpaRepository<Message, Long> {
List<Message> findBySenderName(String senderName);
}
\ No newline at end of file
package ntnu.idatt2105.group44.trivioServer.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ntnu.idatt2105.group44.trivioServer.model.Message;
import ntnu.idatt2105.group44.trivioServer.repository.MessageRepository;
import java.util.List;
@Service
public class MessageService {
private final MessageRepository messageRepository;
@Autowired
public MessageService(MessageRepository messageRepository) {
this.messageRepository = messageRepository;
}
public Message createMessage(Message message) {
return messageRepository.save(message);
}
public List<Message> getAllMessages() {
return messageRepository.findAll();
}
}
......@@ -31,7 +31,14 @@ public class UserService {
return userRepository.findAll();
}
public void addUser(User user){
userRepository.save(user);
public User createUser(String username, String password, String email) {
User user = new User(username, password, email);
return userRepository.save(user);
}
public boolean validateUser(String username, String password) {
Optional<User> user = userRepository.findUserByUsername(username);
return user.isPresent() && user.get().getPassword().equals(password);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment