Skip to content

Commit

Permalink
INSERT operation done
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshat120 committed Aug 15, 2024
1 parent b984a67 commit c8d318a
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 13 deletions.
15 changes: 12 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func main() {
// TODO: use a config struct to maintain env variables
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
Expand All @@ -24,16 +25,24 @@ func main() {

taskRepo := postgres.NewTaskRepo(dbconn)

tmpl := template.Must(template.ParseFiles("templates/task.html"))
taskHandler := handler.NewTaskHandler(tmpl, taskRepo)
previewTaskTemplate := template.Must(template.ParseFiles("templates/preview_task.html"))
previewTaskHandler := handler.NewPreviewTaskHandler(previewTaskTemplate, taskRepo)

createTaskTemplate := template.Must(template.ParseFiles("templates/new_task.html"))
createTaskHandler := handler.NewCreateTaskHandler(createTaskTemplate, taskRepo)

r := mux.NewRouter()

r.HandleFunc("/health", api.HealthCheckHandler)

r.HandleFunc("/", api.HealthCheckHandler)

r.HandleFunc("/task", taskHandler.Handle)
r.HandleFunc("/preview", previewTaskHandler.Handle).Methods("GET")
r.HandleFunc("/new", createTaskHandler.HandleShowForm).Methods("GET")
r.HandleFunc("/new", createTaskHandler.HandleCreateTask).Methods("POST")
// TODO: list_task, update and delete
// r.HandleFunc("/update", ).Methods("POST")
// r.HandleFunc("/delete", ).Methods("POST")

// Start the server
log.Println("Starting server on :8080")
Expand Down
55 changes: 55 additions & 0 deletions internal/handler/create_task_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package handler

import (
"fmt"
"net/http"
"text/template"
"time"

"github.com/Akshat120/Task-Management-System/internal/repos"
)

type createTaskHandler struct {
tmpl *template.Template
repo repos.TaskRepo
}

func NewCreateTaskHandler(tmpl *template.Template, repo repos.TaskRepo) *createTaskHandler {
return &createTaskHandler{repo: repo, tmpl: tmpl}
}

func (handler *createTaskHandler) HandleShowForm(w http.ResponseWriter, r *http.Request) {
if err := handler.tmpl.Execute(w, nil); err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
}
}

func (handler *createTaskHandler) HandleCreateTask(w http.ResponseWriter, r *http.Request) {
var due_date time.Time
var err error
layout := "2006-01-02"
if r.FormValue("due_date") != "" {
due_date, err = time.Parse(layout, r.FormValue("due_date"))
if err != nil {
fmt.Println("err:", err)
http.Error(w, "Error in parsing due_date", http.StatusInternalServerError)
return
}
}

task := repos.Task{
Title: r.FormValue("title"),
Description: r.FormValue("description"),
Status: r.FormValue("status"),
DueDate: &due_date,
}

drn_id, err := handler.repo.Upsert(&task)
if err != nil {
fmt.Println("err:", err)
http.Error(w, "Error in creating task", http.StatusInternalServerError)
return
}

http.Redirect(w, r, fmt.Sprintf("/preview?id=%v", drn_id.String()), http.StatusMovedPermanently)
}
1 change: 1 addition & 0 deletions internal/handler/delete_task_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package handler
2 changes: 1 addition & 1 deletion internal/handler/preview_task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PreviewTaskHandler struct {
tmpl *template.Template
}

func NewTaskHandler(tmpl *template.Template, repo repos.TaskRepo) *PreviewTaskHandler {
func NewPreviewTaskHandler(tmpl *template.Template, repo repos.TaskRepo) *PreviewTaskHandler {
return &PreviewTaskHandler{tmpl: tmpl, repo: repo}
}

Expand Down
1 change: 1 addition & 0 deletions internal/handler/update_task_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package handler
15 changes: 13 additions & 2 deletions internal/postgres/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func (t taskPgRepo) Delete(uuid.UUID) (bool, error) {
}

// Upsert implements repos.TaskRepo.
func (t taskPgRepo) Upsert(*repos.Task) (uuid.UUID, error) {
panic("unimplemented")
func (t taskPgRepo) Upsert(task *repos.Task) (uuid.UUID, error) {
_, err := t.DB.Model(task).
OnConflict("(drn_id) DO UPDATE").
Set("title = EXCLUDED.title").
Set("description = EXCLUDED.description").
Set("status = EXCLUDED.status").
Set("due_date = EXCLUDED.due_date").
Returning("drn_id").
Insert()
if err != nil {
return uuid.Nil, err
}
return task.DrnId, nil
}
10 changes: 5 additions & 5 deletions internal/repos/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type Task struct {
DrnID uuid.UUID
Title string
Description string
Status string
DueDate *time.Time
DrnId uuid.UUID `json:"drn_id"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Status string `json:"status"`
DueDate *time.Time `json:"due_date,omitempty"`
}

type TaskRepo interface {
Expand Down
89 changes: 89 additions & 0 deletions templates/new_task.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Task</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
max-width: 600px;
margin: 0 auto;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input,
.form-group textarea,
.form-group select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-group button {
padding: 10px 20px;
background-color: #28a745;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
.form-group button:hover {
background-color: #218838;
}
</style>
</head>
<body>

<div class="container">
<h1>Create New Task</h1>
<form action="/new" method="POST">
<div class="form-group">
<label for="title">Task Title</label>
<input type="text" id="title" name="title" required>
</div>

<div class="form-group">
<label for="description">Task Description</label>
<textarea id="description" name="description" rows="4"></textarea>
</div>

<div class="form-group">
<label for="status">Task Status</label>
<select id="status" name="status" required>
<option value="Pending">Pending</option>
<option value="In Progress">In Progress</option>
<option value="Completed">Completed</option>
</select>
</div>

<div class="form-group">
<label for="due_date">Due Date</label>
<input type="date" id="due_date" name="due_date">
</div>

<div class="form-group">
<button type="submit">Create Task</button>
</div>
</form>
</div>

</body>
</html>
4 changes: 2 additions & 2 deletions templates/task.html → templates/preview_task.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<h1>Task Details</h1>
<div class="task-detail">
<label for="id">ID:</label>
<span class="value">{{ .DrnID }}</span>
<span class="value">{{ .DrnId }}</span>
</div>
<div class="task-detail">
<label for="title">Title:</label>
Expand All @@ -52,7 +52,7 @@ <h1>Task Details</h1>
</div>
<div class="task-detail">
<label for="due-date">Due Date:</label>
<span class="value">{{ if .DueDate }}{{ .DueDate.Format "2006-01-02 15:04:05" }}{{ else }}Not Set{{ end }}</span>
<span class="value">{{ if .DueDate }}{{ .DueDate.Format "2006-01-02" }}{{ else }}Not Set{{ end }}</span>
</div>
</div>
</body>
Expand Down

0 comments on commit c8d318a

Please sign in to comment.