TaskSubmissionService Microservice

In this microservice, we can develop the microservice can be manages the submission of the tasks by users and handles the related functionalities such as validation and processing.

Create the spring project using spring STS IDE on creating the spring project adding the below dependencies into the spring project.

Dependencies:

  • Spring Web
  • Spring data for mongodb
  • Spring Netflix Eureka client server
  • OpenFeign
  • Spring Dev Tools
  • Lombok
  • Zipkin server

Once create the spring project includes the mention dependencies into the project after creating the project then the file structure looks like the below image.

File structure:

Open the application.properties file and put the below code for the database configuration, server port assigning and Zipkin server configurations into the project

server.port=8083
spring.data.mongodb.uri=mongodb://localhost:27017/userSubmission
spring.application.name=TASK-SUBMISSION
logging.level.org.springframework=DEBUG
eureka.instance.prefer-ip-address=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone = http://localhost:8085/eureka
#Zipkin server configuration
spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0

Step 1 : Create the package in in.mahesh.tasks location and it named as the submissionModel and create the class in that package named as TaskSubmission

Go to src > java > in.mahesh.tasks > submissionModel > TaskSubmission and put the code below:

Java




package in.mahesh.tasks.submissionModel;
  
import java.time.LocalDateTime;
  
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
  
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
  
@Document(collection = "taskSubmission")
@AllArgsConstructor
@NoArgsConstructor
public class TaskSubmission {
      
    @Id
    private String id;
      
    private String taskId;
      
    private String githubLink;
      
    private String userId;
      
    private String status = "PENDING";
      
    private LocalDateTime submissionTime;
      
      
  
    public LocalDateTime getSubmissionTime() {
        return submissionTime;
    }
  
    public void setSubmissionTime(LocalDateTime submissionTime) {
        this.submissionTime = submissionTime;
    }
  
    public String getId() {
        return id;
    }
  
    public void setId(String id) {
        this.id = id;
    }
  
    public String getTaskId() {
        return taskId;
    }
  
    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
  
    public String getGithubLink() {
        return githubLink;
    }
  
    public void setGithubLink(String githubLink) {
        this.githubLink = githubLink;
    }
  
    public String getUserId() {
        return userId;
    }
  
    public void setUserId(String userId) {
        this.userId = userId;
    }
  
    public String getStatus() {
        return status;
    }
  
    public void setStatus(String status) {
        this.status = status;
    }
      
}


Step 2 : In same package, We can create one more class and it named as TaskDTO

Go to src > java > in.mahesh.tasks > submissionModel > TaskDTO and put the code below:

Java




package in.mahesh.tasks.submissionModel;
  
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
  
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
  
@AllArgsConstructor
@NoArgsConstructor
public class TaskDTO {
      
    private String id;
    private String title;
    private String description;
    private String image;
    //private String assignedUserId;
    private String status;
    private LocalDateTime deadline;
    private LocalDateTime createAt;
    //private List<String> tags = new ArrayList<>();
    public String getId() {
        return id;
    }
    public void setId(String 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 String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public LocalDateTime getDeadline() {
        return deadline;
    }
    public void setDeadline(LocalDateTime deadline) {
        this.deadline = deadline;
    }
    public LocalDateTime getCreateAt() {
        return createAt;
    }
    public void setCreateAt(LocalDateTime createAt) {
        this.createAt = createAt;
    }    
  
}


Step 3 : In same package, We can create one more class and it named as UserDTO

Go to src > java > in.mahesh.tasks > submissionModel > UserDTO and put the code below:

Java




package in.mahesh.tasks.submissionModel;
  
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
  
@AllArgsConstructor
@NoArgsConstructor
public class UserDTO {
      
     private String id;
        private String fullName;
        private String email;
        //private String password;
        private String role;
        private String mobile;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getFullName() {
            return fullName;
        }
        public void setFullName(String fullName) {
            this.fullName = fullName;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
      
        public String getRole() {
            return role;
        }
        public void setRole(String role) {
            this.role = role;
        }
        public String getMobile() {
            return mobile;
        }
        public void setMobile(String mobile) {
            this.mobile = mobile;
        }        
  
}


Step 4 : In same package, We can create enum and it named as TaskStatus

Go to src > java > in.mahesh.tasks > submissionModel > TaskStatus and put the code below:

Java




package in.mahesh.tasks.submissionModel;
  
public enum TaskStatus {
    PENDING("PENDING"),
    ASSIGNED("ASSIGNED"),
    DONE("DONE");
      
    TaskStatus(String done) {
          
    }
  
}


Step 5: Create the package in in.mahesh.tasks location and it named as the repository and create the class in that package named as SubRepository

Go to src > java > in.mahesh.tasks > repository > SubRepository and put the code below:

Java




package in.mahesh.tasks.repository;
  
import java.util.List;
  
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
  
import in.mahesh.tasks.submissionModel.TaskSubmission;
  
@Repository
public interface SubRepository extends MongoRepository<TaskSubmission,String>{
      
    //List<TaskSubmission> findByTaskId(String taskId);
  
}


Step 6: Create the package in in.mahesh.tasks location and it named as the service and create the class in that package named as SubmissionService

Go to src > java > in.mahesh.tasks > service> SubmissionService and put the code below:

Java




package in.mahesh.tasks.service;
  
import java.util.List;
  
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
  
import in.mahesh.tasks.submissionModel.TaskSubmission;
  
@Component
public interface SubmissionService {
  
      
      TaskSubmission submitTask(String taskId, String githubLink, String userId,String jwt) throws Exception;
        
      TaskSubmission getTaskSubmissionById(String submissionId) throws Exception;
        
     List<TaskSubmission> getAllTaskSubmissions(); 
        
      List<TaskSubmission> getTaskSubmissionByTaskId(String taskId) ;
        
      TaskSubmission acceptDeclineSubmission(String id, String status) throws Exception;
       
}


Step 7: In same package, We can create one more interface and it named as SubmissionSerivce.

Go to src > java > in.mahesh.tasks > service > SubmissionSerivce and put the code below:

Java




package in.mahesh.tasks.service;
  
import java.util.List;
  
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
  
import in.mahesh.tasks.submissionModel.TaskSubmission;
  
@Component
public interface SubmissionService {
  
      
      TaskSubmission submitTask(String taskId, String githubLink, String userId,String jwt) throws Exception;
        
      TaskSubmission getTaskSubmissionById(String submissionId) throws Exception;
        
     List<TaskSubmission> getAllTaskSubmissions(); 
        
      List<TaskSubmission> getTaskSubmissionByTaskId(String taskId) ;
        
      TaskSubmission acceptDeclineSubmission(String id, String status) throws Exception;
       
}


Step 8: In same package, we can create one more interface and it named as TaskSerivce.

Go to src > java > in.mahesh.tasks > service > TaskSerivce and put the code below:

Java




package in.mahesh.tasks.service;
  
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
  
import in.mahesh.tasks.submissionModel.TaskDTO;
  
  
@FeignClient(name = "TASK-SERVICE",url = "http://localhost:8082")
public interface TaskService {
  
  
    @GetMapping("/api/tasks/{id}")
    TaskDTO getTaskById(@PathVariable String id, @RequestHeader("Authorization") String jwt) throws Exception;
  
  
    @PutMapping("/api/tasks/{id}/complete")
    TaskDTO completeTask(@PathVariable
                         String id);
  
}


Step 9: In same package, we can create one more interface and it named as UserService.

Go to src > java > in.mahesh.tasks > service > UserService and put the code below:

Java




package in.mahesh.tasks.service;
  
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
  
import in.mahesh.tasks.submissionModel.UserDTO;
  
  
  
//connect with taskUserService microService
@FeignClient(name = "USER-SERVICE",url = "http://localhost:8081")
public interface UserService {
      
  
    @GetMapping("/api/users/profile")
    public UserDTO getUserProfileHandler(@RequestHeader("Authorization") String jwt);
  
  
}


Step 10 : Create the package in in.mahesh.tasks location and it named as the controller and create the class in that package named as HomeController

Go to src > java > in.mahesh.tasks > controller > HomeController and put the code below:

Java




package in.mahesh.tasks.controller;
  
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
  
@RestController
public class HomeController {
      
      
    @GetMapping("/submissions")
    public ResponseEntity<String> homeController() {
        return new ResponseEntity<>("Welcome to Task Submission Service",HttpStatus.OK);
    }
  
}


Step 11: In same package, We can create one more interface and it named as SubController.

Go to src > java > in.mahesh.tasks > controller > SubController and put the code below:

Java




package in.mahesh.tasks.controller;
  
import java.util.List;
  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
  
import in.mahesh.tasks.service.SubmissionService;
import in.mahesh.tasks.service.TaskService;
import in.mahesh.tasks.service.UserService;
import in.mahesh.tasks.submissionModel.TaskSubmission;
import in.mahesh.tasks.submissionModel.UserDTO;
import jakarta.inject.Qualifier;
  
  
@RestController
@RequestMapping("/api/submissions")
public class SubController {
  
    @Autowired
    private TaskService taskService;
  
    @Autowired
    private SubmissionService submissionService;
  
    @Autowired
    private UserService userService;
  
  
    public ResponseEntity<TaskSubmission> submitTask(@RequestParam String task_id,
                                                     @RequestParam String github_link, @RequestHeader("Authorization") String jwt) throws Exception {
        try {
            UserDTO user = userService.getUserProfileHandler(jwt);
            TaskSubmission sub = submissionService.submitTask(task_id, github_link, user.getId(), jwt);
  
            return new ResponseEntity<>(sub, HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            throw e; // Re-throwing the exception to propagate it up
        }
    }
  
    @GetMapping("/{submissionId}")
    public ResponseEntity<TaskSubmission> getTaskSubmissionById(@PathVariable String id) throws Exception {
        try {
            TaskSubmission sub = submissionService.getTaskSubmissionById(id);
            return new ResponseEntity<>(sub, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            throw e; // Re-throwing the exception to propagate it up
        }
    }
  
    @GetMapping()
    public ResponseEntity<List<TaskSubmission>> getAllTaskSubmissions() throws Exception {
        try {
            List<TaskSubmission> sub = submissionService.getAllTaskSubmissions();
            return new ResponseEntity<>(sub, HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            throw e; // Re-throwing the exception to propagate it up
        }
    }
  
    @GetMapping("/task/{taskId}")
    public ResponseEntity<List<TaskSubmission>> getTaskSubmissionsByTaskId(@PathVariable String taskId) {
        try {
            List<TaskSubmission> submissions = submissionService.getTaskSubmissionByTaskId(taskId);
            return new ResponseEntity<>(submissions, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            throw e; // Re-throwing the exception to propagate it up
        }
    }
  
  
    @PutMapping("/{id}")
    public ResponseEntity<TaskSubmission>
    acceptOrDeclineTaskSubmission(
  
            @PathVariable String id,
  
            @RequestParam("status") String status) throws Exception {
        try {
            TaskSubmission submission = submissionService.acceptDeclineSubmission(id,
                    status);
  
            if (submission.getStatus().equals("COMPLETE")) {
                taskService.completeTask(submission.getTaskId());
            }
  
            return new ResponseEntity<>(submission, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            throw e; // Re-throwing the exception to propagate it
        }
    }
  
}


Step 12: Open the main class and in that class enables the feign clients using @EnableFeignClients annotation.

Java




package in.mahesh.tasks;
  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
  
@SpringBootApplication
@EnableFeignClients
public class TaskSubmissionServiceApplication {
  
    public static void main(String[] args) {
        SpringApplication.run(TaskSubmissionServiceApplication.class, args);
    }
  
}


Step 13: Once completed the project and run the application as spring boot app and once the project runs successfully and it open the port number 8083. Refer the below image for the better understanding.

Endpoints of the TaskSubmissionService microservice:

Method

Endpoints

GET

http://localhost:8083/submissions

GET

http://localhost:8083/api/submissions/{submissionId}

GET

http://localhost:8083/api/submissions

GET

http://localhost:8083/api/submissions/task/{taskId}

PUT

http://localhost:8083/api/submissions/{id}

Create a Backend Task Management System using Microservices

Microservices is an architectural pattern, and its approach can be used in software development where the large applications are built as a collection of loosely coupled and independent deployable services. All microservices communicate with each other using a well-defined API, typically using lightweight protocols using HTTP.

Developing the backend task management system using microservices involves breaking down the smaller functionalities into independent services that can communicate with each other using the eureka server and implementing the API Gateway for routing client requests.

In this project, we can develop the 5 individual microservices and communicate with each other.

  • Task User Service
  • Task Creation Service
  • Task Submission Service
  • Eureka Server configuration
  • API Gateway

TaskUserService:

In this microservice, we can develop the microservice for responsible for managing the user-related operations such as user authentication, authorization and user profile management. In this service we can develop the USER and ADMIN role then admin can be able to create the tasks and approve the tasks of the user.

TaskCreationService:

In this microservice, we can develop the microservice for creating the task and modification of the tasks and assigning the users and one more thing only admin can create the tasks and user unable to create the tasks.

TaskSubmissionService:

In this microservice, we can develop the microservice can be manages the submission of the tasks by users and handles the related functionalities such as validation and processing.

EurekaServerConfiguration:

In this microservice, we can develop the microservice and it can be used to establish the communication of the microservices using eureka server using discovery client and server with communicates with each dynamically.

APIGatway:

In this microservice, we can develop the microservice and it can be used to API Gateway and establish the routing the client requests of the system.

Similar Reads

TaskUserService Microservice:

In this microservice, we can develop the microservice for responsible for managing the user-related operations such as user authentication, authorization and user profile management. In this service we can develop the USER and ADMIN role then admin can able to create the tasks and approve the tasks of the user....

TaskService Microservice

...

TaskSubmissionService Microservice:

...

EurekaServerConfiguration microservice:

...

APIGatway microservice:

...

Contact Us