diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e644113 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..b82aa23 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..6405a2c --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'GDSC_Backend' diff --git a/src/main/java/com/example/GDSC_Backend/GdscBackendApplication.java b/src/main/java/com/example/GDSC_Backend/GdscBackendApplication.java new file mode 100644 index 0000000..9d0dba5 --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/GdscBackendApplication.java @@ -0,0 +1,11 @@ +package com.example.GDSC_Backend; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GdscBackendApplication { + public static void main(String[] args) { + SpringApplication.run(GdscBackendApplication.class, args); + } +} diff --git a/src/main/java/com/example/GDSC_Backend/controller/TaskController.java b/src/main/java/com/example/GDSC_Backend/controller/TaskController.java new file mode 100644 index 0000000..507a18e --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/controller/TaskController.java @@ -0,0 +1,60 @@ +package com.example.GDSC_Backend.controller; + +import com.example.GDSC_Backend.model.Task; +import com.example.GDSC_Backend.repository.TaskRepository; +import com.example.GDSC_Backend.service.TaskService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Controller +@RequestMapping("/tasks") +public class TaskController { + private final TaskService taskService; + + @Autowired + public TaskController(TaskService taskService) { + this.taskService = new TaskService(); + } + + @PostMapping + @ResponseBody + public Task createTask(@RequestParam String title, @RequestParam String description, @RequestParam LocalDate dueDate, @RequestParam boolean status) { + return taskService.createTask(title, description, dueDate, status); + } + + @GetMapping("/{id}") + @ResponseBody + public Task getTask(@PathVariable Long id) { + Optional task = taskService.getTask(id); + return task.orElse(null); + } + + @GetMapping + @ResponseBody + public List getAllTasks() { + return taskService.getAllTasks(); + } + + @PutMapping("/{id}") + @ResponseBody + public Task updateTask(@PathVariable Long id, @RequestParam String title, @RequestParam String description, @RequestParam LocalDate dueDate, @RequestParam boolean status) { + return taskService.updateTask(id, title, description, dueDate, status); + } + + @DeleteMapping("/{id}") + @ResponseBody + public void deleteTask(@PathVariable Long id) { + taskService.deleteTask(id); + } +} diff --git a/src/main/java/com/example/GDSC_Backend/controller/UserController.java b/src/main/java/com/example/GDSC_Backend/controller/UserController.java new file mode 100644 index 0000000..e9db3da --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/controller/UserController.java @@ -0,0 +1,51 @@ +package com.example.GDSC_Backend.controller; + +import com.example.GDSC_Backend.model.User; +import com.example.GDSC_Backend.repository.UserRepository; +import com.example.GDSC_Backend.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; + +@Controller +@RequestMapping("/users") +public class UserController { + private final UserService userService; + + @Autowired + public UserController(UserService userService) { + this.userService = new UserService(); + } + + @PostMapping + @ResponseBody + public User createUser(@RequestParam String name, @RequestParam String email, @RequestParam String password) { + return userService.createUser(name, email, password); + } + + @GetMapping("/{id}") + @ResponseBody + public User getUser(@PathVariable Long id) { + Optional user = userService.getUser(id); + return user.orElse(null); + } + + @PutMapping("/{id}") + @ResponseBody + public User updateUser(@PathVariable Long id, @RequestParam String name, @RequestParam String email, @RequestParam String password) { + return userService.updateUser(id, name, email, password); + } + + @DeleteMapping("/{id}") + @ResponseBody + public void deleteUser(@PathVariable Long id) { + userService.deleteUser(id); + } +} diff --git a/src/main/java/com/example/GDSC_Backend/model/Task.java b/src/main/java/com/example/GDSC_Backend/model/Task.java new file mode 100644 index 0000000..6fb7e38 --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/model/Task.java @@ -0,0 +1,62 @@ +package com.example.GDSC_Backend.model; + +import java.time.LocalDate; + +public class Task { + private Long id; + private String title; + private String description; + private LocalDate dueDate; + private boolean status; + + // Constructors + public Task() {} + + public Task(String title, String description, LocalDate dueDate, boolean status) { + this.title = title; + this.description = description; + this.dueDate = dueDate; + this.status = status; + } + + // Getters and setters + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LocalDate getDueDate() { + return dueDate; + } + + public void setDueDate(LocalDate dueDate) { + this.dueDate = dueDate; + } + + public boolean isStatus() { + return status; + } + + public void setStatus(boolean status) { + this.status = status; + } +} diff --git a/src/main/java/com/example/GDSC_Backend/model/User.java b/src/main/java/com/example/GDSC_Backend/model/User.java new file mode 100644 index 0000000..cb2e14c --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/model/User.java @@ -0,0 +1,50 @@ +package com.example.GDSC_Backend.model; + +public class User { + private Long id; + private String name; + private String email; + private String password; + + // Constructors + public User() {} + + public User(String name, String email, String password) { + this.name = name; + this.email = email; + this.password = password; + } + + // Getters and setters + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } +} diff --git a/src/main/java/com/example/GDSC_Backend/repository/TaskRepository.java b/src/main/java/com/example/GDSC_Backend/repository/TaskRepository.java new file mode 100644 index 0000000..1d03cf3 --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/repository/TaskRepository.java @@ -0,0 +1,34 @@ +package com.example.GDSC_Backend.repository; + +import com.example.GDSC_Backend.model.Task; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; + +public class TaskRepository { + private List tasks = new ArrayList<>(); + private AtomicLong nextId = new AtomicLong(1); + + public Task save(Task task){ + if (task.getId() == null){ + task.setId(nextId.getAndIncrement()); + } + tasks.add(task); + return task; + } + + public Optional findById(Long id) { + return tasks.stream().filter(task -> task.getId().equals(id)).findFirst(); + } + + public List findAll() { + return new ArrayList<>(tasks); + } + + public void deleteById(Long id) { + tasks.removeIf(task -> task.getId().equals(id)); + } +} diff --git a/src/main/java/com/example/GDSC_Backend/repository/UserRepository.java b/src/main/java/com/example/GDSC_Backend/repository/UserRepository.java new file mode 100644 index 0000000..27a1974 --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/repository/UserRepository.java @@ -0,0 +1,33 @@ +package com.example.GDSC_Backend.repository; + +import com.example.GDSC_Backend.model.User; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; + +public class UserRepository { + private List users = new ArrayList<>(); + private AtomicLong nextId = new AtomicLong(1); + + public User save(User user){ + if (user.getId() == null){ + user.setId((nextId.getAndIncrement())); + } + users.add(user); + return user; + } + + public Optional findById(Long id){ + return users.stream().filter(user -> user.getId().equals(id)).findFirst(); + } + + public List findAll(){ + return new ArrayList<>(users); + } + + public void deleteById(long id){ + users.removeIf(user -> user.getId().equals(id)); + } +} diff --git a/src/main/java/com/example/GDSC_Backend/service/TaskService.java b/src/main/java/com/example/GDSC_Backend/service/TaskService.java new file mode 100644 index 0000000..b088bc1 --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/service/TaskService.java @@ -0,0 +1,48 @@ +package com.example.GDSC_Backend.service; + +import com.example.GDSC_Backend.model.Task; +import com.example.GDSC_Backend.repository.TaskRepository; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; + +@Service +public class TaskService { + private final TaskRepository taskRepository; + + public TaskService() { + this.taskRepository = new TaskRepository(); + } + + public Task createTask(String title, String description, LocalDate dueDate, boolean status) { + Task task = new Task(title, description, dueDate, status); + return taskRepository.save(task); + } + + public Optional getTask(Long id) { + return taskRepository.findById(id); + } + + public List getAllTasks() { + return taskRepository.findAll(); + } + + public Task updateTask(Long id, String title, String description, LocalDate dueDate, boolean status) { + Optional optionalTask = taskRepository.findById(id); + if (optionalTask.isPresent()) { + Task task = optionalTask.get(); + task.setTitle(title); + task.setDescription(description); + task.setDueDate(dueDate); + task.setStatus(status); + return taskRepository.save(task); + } + return null; + } + + public void deleteTask(Long id) { + taskRepository.deleteById(id); + } +} diff --git a/src/main/java/com/example/GDSC_Backend/service/UserService.java b/src/main/java/com/example/GDSC_Backend/service/UserService.java new file mode 100644 index 0000000..4d77620 --- /dev/null +++ b/src/main/java/com/example/GDSC_Backend/service/UserService.java @@ -0,0 +1,41 @@ +package com.example.GDSC_Backend.service; + +import com.example.GDSC_Backend.model.User; +import com.example.GDSC_Backend.repository.UserRepository; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class UserService { + private final UserRepository userRepository; + + public UserService() { + this.userRepository = new UserRepository(); + } + + public User createUser(String name, String email, String password) { + User user = new User(name, email, password); + return userRepository.save(user); + } + + public Optional getUser(Long id) { + return userRepository.findById(id); + } + + public User updateUser(Long id, String name, String email, String password) { + Optional optionalUser = userRepository.findById(id); + if (optionalUser.isPresent()) { + User user = optionalUser.get(); + user.setName(name); + user.setEmail(email); + user.setPassword(password); + return userRepository.save(user); + } + return null; + } + + public void deleteUser(Long id) { + userRepository.deleteById(id); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..32d32c4 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=GDSC_Backend diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 0000000..8d2afce --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +

hello

+ + \ No newline at end of file diff --git a/src/test/java/com/example/GDSC_Backend/GdscBackendApplicationTests.java b/src/test/java/com/example/GDSC_Backend/GdscBackendApplicationTests.java new file mode 100644 index 0000000..9a6b7af --- /dev/null +++ b/src/test/java/com/example/GDSC_Backend/GdscBackendApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.GDSC_Backend; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class GdscBackendApplicationTests { + + @Test + void contextLoads() { + } + +}