parent
8fedac21bb
commit
d1774c1ce0
@ -268,6 +268,36 @@ func TestCreateTask(t *testing.T) {
|
|||||||
responseWithUser.Value("id").Number().NotEqual(taskId) // Ensure different ID
|
responseWithUser.Value("id").Number().NotEqual(taskId) // Ensure different ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeleteTask(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
|
||||||
|
// Create a new task without assigned user
|
||||||
|
requestBody := map[string]interface{}{
|
||||||
|
"name": "Test Task",
|
||||||
|
"reward": 100,
|
||||||
|
}
|
||||||
|
|
||||||
|
response := e.POST("/tasks").
|
||||||
|
WithJSON(requestBody).
|
||||||
|
Expect().
|
||||||
|
Status(201). // Expect Created status
|
||||||
|
JSON().Object()
|
||||||
|
|
||||||
|
// Verify the response has an ID
|
||||||
|
response.ContainsKey("id")
|
||||||
|
taskId := response.Value("id").Number().Raw()
|
||||||
|
|
||||||
|
// Delete the task
|
||||||
|
e.DELETE("/task/" + strconv.Itoa(int(taskId))).Expect().Status(200)
|
||||||
|
// Verify the task no longer exists
|
||||||
|
e.GET("/task/" + strconv.Itoa(int(taskId))).Expect().Status(404)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteTaskNotFound(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
e.DELETE("/task/1").Expect().Status(404)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateTaskNoName(t *testing.T) {
|
func TestCreateTaskNoName(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
|
|
||||||
|
@ -221,6 +221,21 @@ func (db *Db) GetTask(id int) (Task, error) {
|
|||||||
return task, nil
|
return task, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Db) DeleteTask(id int) error {
|
||||||
|
tx, err := db.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.MustRollback()
|
||||||
|
|
||||||
|
err = tx.Query("delete from tasks where id = ?").Bind(id).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
func (db *Db) HasTask(id int) (bool, error) {
|
func (db *Db) HasTask(id int) (bool, error) {
|
||||||
count := 0
|
count := 0
|
||||||
err := db.db.Query("select count(*) from tasks where id = ?").
|
err := db.db.Query("select count(*) from tasks where id = ?").
|
||||||
|
@ -333,6 +333,37 @@ func putTask(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
|
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteTask(c *gin.Context) {
|
||||||
|
taskIdStr := c.Param("taskId")
|
||||||
|
taskId, err := strconv.Atoi(taskIdStr)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Invalid task ID: %v", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid task ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hasTask, err := db.HasTask(taskId)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error checking task existence: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasTask {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Task not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.DeleteTask(taskId)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error deleting task: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Task deleted successfully"})
|
||||||
|
}
|
||||||
|
|
||||||
func postHistory(c *gin.Context) {
|
func postHistory(c *gin.Context) {
|
||||||
userIdStr := c.Param("userId")
|
userIdStr := c.Param("userId")
|
||||||
userId, err := strconv.Atoi(userIdStr)
|
userId, err := strconv.Atoi(userIdStr)
|
||||||
@ -412,6 +443,7 @@ func start(ctx context.Context, config *ServerConfig) {
|
|||||||
router.GET("/api/tasks", getTasks)
|
router.GET("/api/tasks", getTasks)
|
||||||
router.GET("/api/task/:taskId", getTask)
|
router.GET("/api/task/:taskId", getTask)
|
||||||
router.PUT("/api/task/:taskId", putTask)
|
router.PUT("/api/task/:taskId", putTask)
|
||||||
|
router.DELETE("/api/task/:taskId", deleteTask)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: config.Addr,
|
Addr: config.Addr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user