Skip to content

Commit

Permalink
Merge pull request #123 from rash0621/main
Browse files Browse the repository at this point in the history
Task
  • Loading branch information
rash0621 authored Oct 10, 2024
2 parents 2263ecb + f25d3fd commit 0b6faeb
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 117 deletions.
4 changes: 4 additions & 0 deletions back-end/src/main/java/com/example/demo/appuser/AppUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public AppUser( String firstName,
* This method is used by Spring Security to determine the roles of the user
*/
@Override
@JsonIgnore
public Collection<? extends GrantedAuthority> getAuthorities() {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(appUserRole.name());
return Collections.singletonList(authority);
Expand All @@ -97,16 +98,19 @@ public String getUsername() {
}

@Override
@JsonIgnore
public boolean isAccountNonExpired() {
return true;
}

@Override
@JsonIgnore
public boolean isAccountNonLocked() {
return !locked;
}

@Override
@JsonIgnore
public boolean isCredentialsNonExpired() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
Expand All @@ -33,4 +30,7 @@ public List<AppUser> getUserList(){
public AppUser getUser(@PathVariable Integer userID){
return appUserService.getUser(userID);
}

@PostMapping("/getUserByEmail")
public AppUser getUserByEmail(@RequestBody EmailRequest email){return appUserService.getUserAndCheckingByEmail(email);}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface AppUserRepository extends JpaRepository<AppUser, Long> {
@Transactional
@Modifying
@Query("UPDATE AppUser a " +
"SET a.enabled = TRUE WHERE a.email = ?1")
"SET a.enabled = TRUE WHERE a.email = ?1")
int enableAppUser(String email);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* Users can have either the USER role or the ADMIN role.
*/
public enum AppUserRole {
USER, ADMIN;
USER, ADMIN, COORDINATOR, MANAGER, SUPER_ADMIN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public List<AppUser> getAllUsers(){
public AppUser getUser(long userID){
return appUserRepository.findById(userID).get();
}

//checking if a user exists
public AppUser getUserAndCheckingByEmail(EmailRequest emailRequest) {
Optional<AppUser> appUser = appUserRepository.findByEmail(emailRequest.getEmail());
return appUser.orElse(null);
}
}


16 changes: 16 additions & 0 deletions back-end/src/main/java/com/example/demo/appuser/EmailRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.demo.appuser;

public class EmailRequest {
private String email;

// Getters and setters
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}


11 changes: 4 additions & 7 deletions back-end/src/main/java/com/example/demo/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class Task {

@Id
//Auto generating values
//Auto generating values
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Task_ID;

Expand Down Expand Up @@ -114,13 +114,10 @@ public void assignMember(AppUser user) {
assignedUsers.add(user);
}

public void deleteAssignMember(AppUser user) {
assignedUsers.remove(user);
public void deleteAssignMembers() {
assignedUsers.clear();
}

public void setAssignedMembers(Set<AppUser> taskMembers){
for (AppUser taskMember : taskMembers) {
assignMember(taskMember);}
}


}
30 changes: 29 additions & 1 deletion back-end/src/main/java/com/example/demo/task/TaskController.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ public TaskResponse addTask(@RequestBody Task newTask){
return new TaskResponse(s);
}

@PostMapping("/addWithUsers")
public ResponseEntity<String> addTaskWithUsers(@RequestBody TaskDTO taskDTO){
try {
Task task = taskDTO.getTask();
Set<AppUser> taskMembers = taskDTO.getTaskMembers();
taskService.addTaskWithUsers(task,taskMembers);
return ResponseEntity.ok("Task added successfully");
}catch (Exception e) {
return ResponseEntity.status(500).body("Failed to add task");
}
}

@PostMapping("/updateWithUsers")
public ResponseEntity<String> updateTaskWithUsers(@RequestBody TaskDTO taskDTO){
try {
Task task = taskDTO.getTask();
Set<AppUser> taskMembers = taskDTO.getTaskMembers();
taskService.updateTaskWithUsers(task,taskMembers);
return ResponseEntity.ok("Task updated successfully");
}catch (Exception e) {
return ResponseEntity.status(500).body("Failed to update task");
}
}

@PutMapping("/update")
public void updateTask(@RequestBody Task updateTask){
taskService.updateTask(updateTask);
Expand All @@ -58,6 +82,10 @@ public TaskResponse addUserToTask(@PathVariable Integer taskID,@PathVariable Lon
return new TaskResponse("Assigned user with id "+userID+" to task with id "+taskID);
}


@DeleteMapping("/{taskID}/users/{userID}")
public TaskResponse deleteUserToTask(@PathVariable Integer taskID,@PathVariable Long userID){
taskService.deleteTaskUsers(taskID,userID);
return new TaskResponse("Deleted user with id "+userID+" to task with id "+taskID);
}

}
29 changes: 29 additions & 0 deletions back-end/src/main/java/com/example/demo/task/TaskDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.demo.task;


import com.example.demo.appuser.AppUser;

import java.util.Optional;
import java.util.Set;

public class TaskDTO {

private Task task;
private Set<AppUser> assignedUsers;

public Task getTask() {
return task;
}

public void setTask(Task task) {
this.task = task;
}

public Set<AppUser> getTaskMembers(){
return assignedUsers;
}

public void setAssignedUsers(Set<AppUser> assignedUsers) {
this.assignedUsers = assignedUsers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public interface TaskRepository extends JpaRepository<Task, Integer> {
@Query("SELECT t FROM Task t where ?1 member of t.assignedUsers")
List<Task> findTasksByUser(AppUser user);

// @Modifying
// @Query("UPDATE Task t SET t.assignedUsers = t.assignedUsers - ?1 WHERE t IN (SELECT t FROM Task t WHERE ?1 member of t.assignedUsers)")
// void deleteUserFromTasks(AppUser user);
@Transactional
@Modifying
@Query(nativeQuery = true, value = "DELETE FROM assigned_tasks where task_id=?1 AND user_id=?2")
void deleteUserFromTasks(int taskID,Long userID);

}

24 changes: 19 additions & 5 deletions back-end/src/main/java/com/example/demo/task/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.demo.appuser.AppUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand All @@ -20,7 +21,7 @@ public class TaskService {


public List<Task> getAllTasks(){
return taskRepository.findAll();
return taskRepository.findAll();
}

public Task getTask(int task_ID){
Expand All @@ -44,12 +45,25 @@ public void addUserToTask(int task_ID, long userID){
taskRepository.save(task);
}

@Transactional
public void addTaskWithUsers(Task task, Set<AppUser> taskMembers){

task.setAssignedMembers(taskMembers);
taskRepository.save(task);
for (AppUser taskMember : taskMembers) {
task.assignMember(taskMember);
}
taskRepository.save(task);

}

@Transactional
public void updateTaskWithUsers(Task task, Set<AppUser> taskMembers){
task.deleteAssignMembers();
for (AppUser taskMember : taskMembers) {
task.assignMember(taskMember);
}
taskRepository.save(task);
}

public void deleteTask(int task_ID){
taskRepository.deleteById(task_ID);
Expand All @@ -61,9 +75,9 @@ public List<Task> findUserTasks(Long userID) {
return taskRepository.findTasksByUser(user);
}

// public void deleteTaskUsers(int taskID,Long userID){
// taskRepository.deleteUserFromTasks(taskID,userID);
// }
public void deleteTaskUsers(int taskID,Long userID){
taskRepository.deleteUserFromTasks(taskID,userID);
}
}


10 changes: 9 additions & 1 deletion back-end/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ server:
spring:
datasource:
url: jdbc:mysql://localhost:3306/cycle
password:
password: "&*531cW9/?"
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
Expand Down Expand Up @@ -35,6 +35,14 @@ spring:
timeout: 3000
write timeout: 5000

security:
oauth2:
client:
registration:
google:
client-id: "257276141541-av8vs5jp5u7e9f3am8dllo761kg7abo8.apps.googleusercontent.com"
client-secret: "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"

servlet:
multipart:
max-file-size: 100MB # Increase size for large files
Expand Down
Loading

0 comments on commit 0b6faeb

Please sign in to comment.