add functionality
This commit is contained in:
1 parent
88fba415dd
commit
62714cefd3
2 files changed
+142
-9
No files matched your search
@@ -1,9 +1,126 @@
|
|||||||
// Task Properties
|
/*
|
||||||
// Each task should have the following properties:
|
# Adding a new task
|
||||||
//
|
task-cli add "Buy groceries"
|
||||||
// id: A unique identifier for the task
|
# Output: Task added successfully (ID: 1)
|
||||||
// description: A short description of the task
|
|
||||||
// status: The status of the task (todo, in-progress, done)
|
# Updating and deleting tasks
|
||||||
// createdAt: The date and time when the task was created
|
task-cli update 1 "Buy groceries and cook dinner"
|
||||||
// updatedAt: The date and time when the task was last updated
|
task-cli delete 1
|
||||||
// Make sure to add these properties to the JSON file when adding a new task and update them when updating a task.
|
|
||||||
|
# Marking a task as in progress or done
|
||||||
|
task-cli mark-in-progress 1
|
||||||
|
task-cli mark-done 1
|
||||||
|
|
||||||
|
# Listing all tasks
|
||||||
|
task-cli list
|
||||||
|
|
||||||
|
# Listing tasks by status
|
||||||
|
task-cli list done
|
||||||
|
task-cli list todo
|
||||||
|
task-cli list in-progress
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Desc string `json:"description"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func fileExists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadTasks(fileName string) ([]Task, error) {
|
||||||
|
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
|
||||||
|
return []Task{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if stat, err := file.Stat(); err != nil || stat.Size() == 0 {
|
||||||
|
return []Task{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var tasks []Task
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
decoder.DisallowUnknownFields()
|
||||||
|
|
||||||
|
if err := decoder.Decode(&tasks); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tasks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveAllTasks(fileName string, tasks []Task) error {
|
||||||
|
file, err := os.Create(fileName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder := json.NewEncoder(file)
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
return encoder.Encode(tasks)
|
||||||
|
}
|
||||||
|
|
||||||
|
func addTask(fileName string, task Task) error {
|
||||||
|
tasks, err := loadTasks(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for duplicates later
|
||||||
|
tasks = append(tasks, task)
|
||||||
|
|
||||||
|
return saveAllTasks(fileName, tasks)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fileName := "tasks.json"
|
||||||
|
|
||||||
|
var task Task
|
||||||
|
task.ID = 3
|
||||||
|
task.Desc = "Hello desc"
|
||||||
|
task.Status = "Todo"
|
||||||
|
task.CreatedAt = time.Now()
|
||||||
|
task.UpdatedAt = time.Now()
|
||||||
|
//
|
||||||
|
var task2 Task
|
||||||
|
task2.ID = 2
|
||||||
|
task2.Desc = "Hello desc 2"
|
||||||
|
task2.Status = "DSfasdfsad"
|
||||||
|
task2.CreatedAt = time.Now()
|
||||||
|
task2.UpdatedAt = time.Now()
|
||||||
|
|
||||||
|
// file, err := openFile(fileName)
|
||||||
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
// defer file.Close()
|
||||||
|
tasks, _ := loadTasks(fileName)
|
||||||
|
addTask(fileName, task)
|
||||||
|
addTask(fileName, task2)
|
||||||
|
for i := range tasks {
|
||||||
|
fmt.Println(tasks[i].ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// addTask(file, task)
|
||||||
|
// addTask(file, task2)
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"description": "Hello desc",
|
||||||
|
"status": "Todo",
|
||||||
|
"createdAt": "2026-11-04T20:55:53.083894-05:00",
|
||||||
|
"updatedAt": "2026-11-04T20:55:53.083894-05:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"description": "Hello desc 2",
|
||||||
|
"status": "DSfasdfsad",
|
||||||
|
"createdAt": "2026-11-04T20:55:53.083894-05:00",
|
||||||
|
"updatedAt": "2026-11-04T20:55:53.083894-05:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
Reference in new issue
Block a user