|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "html/template" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | +) |
| 12 | + |
| 13 | +type ProjectStep struct { |
| 14 | + Id string |
| 15 | + ProjectId string |
| 16 | + StepNumber int |
| 17 | + Description string |
| 18 | +} |
| 19 | + |
| 20 | +func (ps *ProjectStep) ToString() string { |
| 21 | + //fmt.Sprintf("ProjectStep {Id: ()\n\tProjectId: ()\n\tStepNumber: ()\n\tDescription: ()\n\t}") |
| 22 | + return fmt.Sprintf("%#v", ps) |
| 23 | +} |
| 24 | + |
| 25 | +func ProjectStepsToString(ps []ProjectStep) string { |
| 26 | + sb := strings.Builder{} |
| 27 | + for i, p := range ps { |
| 28 | + sb.WriteString(fmt.Sprint(i) + ": " + p.ToString() + "\n") |
| 29 | + } |
| 30 | + |
| 31 | + return sb.String() |
| 32 | +} |
| 33 | + |
| 34 | +type Project struct { |
| 35 | + Name string |
| 36 | + Description string |
| 37 | + Id string |
| 38 | + Steps []ProjectStep |
| 39 | + WithEditRow bool |
| 40 | +} |
| 41 | + |
| 42 | +func (p *Project) ToString() string { |
| 43 | + return "Project {\n\tName: " + p.Name + "\n\tDescription: " + p.Description + "\n\tId: " + p.Id + "\n\tCompletionSteps: []ProjectStep{" + ProjectStepsToString(p.Steps) + "}\n}" |
| 44 | +} |
| 45 | + |
| 46 | +type ProjectViewModel struct { |
| 47 | + Projects []Project |
| 48 | + SelectedProject Project |
| 49 | +} |
| 50 | + |
| 51 | +func panicIfErr(e error) { |
| 52 | + if e != nil { |
| 53 | + panic(e) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func parse(x string) (int, error) { |
| 58 | + return strconv.Atoi(x) |
| 59 | +} |
| 60 | + |
| 61 | +func add(x, y int) string { |
| 62 | + return strconv.Itoa(x + y) |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +func main() { |
| 67 | + mux := http.NewServeMux() |
| 68 | + repo := NewRepository() |
| 69 | + funcMap := template.FuncMap{ |
| 70 | + "add": add, |
| 71 | + "parse": parse, |
| 72 | + } |
| 73 | + |
| 74 | + templates := template.Must(template.New("stupidfuckingbitch").Funcs(funcMap).ParseFiles("main.html", "projects-list.html", "projects-description.html", "projects-steps-table.html")) |
| 75 | + mux.HandleFunc("POST /projects/", func(w http.ResponseWriter, r *http.Request) { |
| 76 | + fmt.Println("Matched POST /projects") |
| 77 | + p := Project{ |
| 78 | + Id: uuid.NewString(), |
| 79 | + Name: r.FormValue("project-name"), |
| 80 | + Description: r.FormValue("project-description"), |
| 81 | + } |
| 82 | + repo.AddProject(p) |
| 83 | + http.Redirect(w, r, r.Referer(), http.StatusSeeOther) |
| 84 | + }) |
| 85 | + |
| 86 | + mux.HandleFunc("DELETE /projects/{id}", func(w http.ResponseWriter, r *http.Request) { |
| 87 | + fmt.Println("Matched DELETE /projects/{id}") |
| 88 | + projectId := r.PathValue("id") |
| 89 | + repo.RemoveProject(projectId) |
| 90 | + http.Redirect(w, r, "/", http.StatusSeeOther) |
| 91 | + }) |
| 92 | + |
| 93 | + mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { |
| 94 | + fmt.Println("Matched GET /") |
| 95 | + selectedProject, err := repo.GetProjectByIndex(0) |
| 96 | + fmt.Println("Selected Project is " + selectedProject.ToString()) |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + templates.ExecuteTemplate(w, "base", ProjectViewModel{Projects: repo.GetAllProjects(), SelectedProject: selectedProject}) |
| 101 | + }) |
| 102 | + |
| 103 | + mux.HandleFunc("GET /projects/{selectedId}", func(w http.ResponseWriter, r *http.Request) { |
| 104 | + selectedId := r.PathValue("selectedId") |
| 105 | + fmt.Println("Matched GET /{selectedId}") |
| 106 | + selectedProj, err := repo.GetProjectById(selectedId) |
| 107 | + if err != nil { |
| 108 | + http.NotFound(w, r) |
| 109 | + } |
| 110 | + projects := repo.GetAllProjects() |
| 111 | + |
| 112 | + templates.ExecuteTemplate(w, "base", ProjectViewModel{Projects: projects, SelectedProject: selectedProj}) |
| 113 | + }) |
| 114 | + |
| 115 | + mux.HandleFunc("GET /projects/{id}/description", func(w http.ResponseWriter, r *http.Request) { |
| 116 | + projectId := r.PathValue("id") |
| 117 | + fmt.Println("Matched GET /projects/" + projectId + "/description") |
| 118 | + proj, err := repo.GetProjectById(projectId) |
| 119 | + if err != nil { |
| 120 | + panic(err) |
| 121 | + } |
| 122 | + |
| 123 | + fmt.Fprint(w, proj.Description) |
| 124 | + }) |
| 125 | + |
| 126 | + mux.HandleFunc("PUT /projects/{id}/description", func(w http.ResponseWriter, r *http.Request) { |
| 127 | + fmt.Println("Matched PUT /projects/{id}/description") |
| 128 | + projDescription := r.FormValue("project-description") |
| 129 | + projId := r.PathValue("id") |
| 130 | + repo.SetProjectDescription(projId, projDescription) |
| 131 | + fmt.Fprint(w, http.StatusOK) |
| 132 | + }) |
| 133 | + |
| 134 | + mux.HandleFunc("GET /project/{id}/steps/", func(w http.ResponseWriter, r *http.Request) { |
| 135 | + projectId := r.PathValue("id") |
| 136 | + fmt.Println("Matched GET /project/" + projectId + "/steps/") |
| 137 | + wer := r.FormValue("WithEditRow") |
| 138 | + WithEditRow, err := strconv.ParseBool(wer) |
| 139 | + fmt.Println("WithEditRow: " + wer) |
| 140 | + if err != nil { |
| 141 | + WithEditRow = false |
| 142 | + } |
| 143 | + proj, err := repo.GetProjectById(projectId) |
| 144 | + panicIfErr(err) |
| 145 | + |
| 146 | + proj.WithEditRow = WithEditRow |
| 147 | + |
| 148 | + newerr := templates.ExecuteTemplate(w, "projectsStepsTable", ProjectViewModel{SelectedProject: proj}) |
| 149 | + if newerr != nil { |
| 150 | + fmt.Fprint(w, "<div style=\"color: red\">"+newerr.Error()+"</div>") |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + mux.HandleFunc("POST /project/{id}/steps/", func(w http.ResponseWriter, r *http.Request) { |
| 155 | + fmt.Println("Matched POST /projectsteps/") |
| 156 | + projectId := r.PathValue("id") |
| 157 | + sn := r.FormValue("stepNumber") |
| 158 | + Description := r.FormValue("Description") |
| 159 | + stepNumber, err := strconv.Atoi(sn) |
| 160 | + panicIfErr(err) |
| 161 | + step := ProjectStep{Id: uuid.NewString(), ProjectId: projectId, StepNumber: stepNumber, Description: Description} |
| 162 | + repo.AddStepToProject(projectId, step) |
| 163 | + http.Redirect(w, r, "/projects/"+projectId, http.StatusSeeOther) |
| 164 | + }) |
| 165 | + |
| 166 | + port := ":8080" |
| 167 | + fmt.Println("Listening on http://localhost" + port) |
| 168 | + http.ListenAndServe(port, mux) |
| 169 | +} |
0 commit comments