Skip to content

Comments

feat: add task api service with GET and POST methods#21

Closed
solar224 wants to merge 1 commit intoandy89923:mainfrom
solar224:feature/add-new-api
Closed

feat: add task api service with GET and POST methods#21
solar224 wants to merge 1 commit intoandy89923:mainfrom
solar224:feature/add-new-api

Conversation

@solar224
Copy link

Hi, My name is Yu-Chen Chan, and I am a first-year master's student in the lab.
As requested by a senior lab member for the Lab 6 exercise, this PR introduces a new API service for managing tasks. It includes the following endpoints:
POST /api/tasks: To create a new task.
GET /api/tasks: To retrieve all existing tasks.

Thank you for your review!

@andy89923 andy89923 requested a review from Copilot September 19, 2025 12:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new REST API service for task management, implementing basic CRUD operations for tasks with GET and POST endpoints at /api/tasks.

  • Adds a Task struct with ID and Name fields to the application context
  • Implements HTTP handlers for creating new tasks and retrieving all tasks
  • Sets up routing infrastructure to expose the task endpoints

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
internal/context/context.go Defines Task struct and adds Tasks slice to NFContext
internal/sbi/processor/task_handler.go Implements business logic for task creation and retrieval
internal/sbi/api_task.go Defines HTTP handlers and route configuration for task endpoints
internal/sbi/router.go Registers task routes under /api group

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +5 to +24

"github.com/andy89923/nf-example/internal/context"
"github.com/gin-gonic/gin"
)

func (p *Processor) CreateNewTask(c *gin.Context) {
var newTask context.Task
if err := c.ShouldBindJSON(&newTask); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
tasks := p.Context().Tasks
newTask.ID = len(tasks) + 1
p.Context().Tasks = append(tasks, newTask)

c.JSON(http.StatusCreated, newTask)
}

func (p *Processor) GetAllTasks(c *gin.Context) {
tasks := p.Context().Tasks
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task creation logic has a race condition where the Tasks slice can be modified by concurrent requests, potentially causing data corruption or lost updates. Consider adding proper synchronization mechanisms like mutexes around the critical section.

Suggested change
"github.com/andy89923/nf-example/internal/context"
"github.com/gin-gonic/gin"
)
func (p *Processor) CreateNewTask(c *gin.Context) {
var newTask context.Task
if err := c.ShouldBindJSON(&newTask); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
tasks := p.Context().Tasks
newTask.ID = len(tasks) + 1
p.Context().Tasks = append(tasks, newTask)
c.JSON(http.StatusCreated, newTask)
}
func (p *Processor) GetAllTasks(c *gin.Context) {
tasks := p.Context().Tasks
"sync"
"github.com/andy89923/nf-example/internal/context"
"github.com/gin-gonic/gin"
)
var tasksMutex sync.Mutex
func (p *Processor) CreateNewTask(c *gin.Context) {
var newTask context.Task
if err := c.ShouldBindJSON(&newTask); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
tasksMutex.Lock()
tasks := p.Context().Tasks
newTask.ID = len(tasks) + 1
p.Context().Tasks = append(tasks, newTask)
tasksMutex.Unlock()
c.JSON(http.StatusCreated, newTask)
}
func (p *Processor) GetAllTasks(c *gin.Context) {
tasksMutex.Lock()
tasks := p.Context().Tasks
tasksMutex.Unlock()

Copilot uses AI. Check for mistakes.
@Alonza0314
Copy link

Hi @solar224 ,
please fork from here and open PR to this repo.

https://github.com/Alonza0314/nf-example

@solar224 solar224 closed this by deleting the head repository Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants