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#21solar224 wants to merge 1 commit intoandy89923:mainfrom solar224:feature/add-new-api
solar224 wants to merge 1 commit intoandy89923:mainfrom
solar224:feature/add-new-api
Conversation
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() |
|
Hi @solar224 , |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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!