Skip to content

Commit

Permalink
Merge pull request hs3city#7 from hs3city/session-8
Browse files Browse the repository at this point in the history
Session 8: CYOA complete implementation
  • Loading branch information
pegre94 authored Jan 17, 2024
2 parents 7f144fe + f02ff56 commit 9b88bf4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
48 changes: 2 additions & 46 deletions cyoa/main/definitions.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,14 @@
package main

type StoryLine struct {
Intro Intro `json:"intro"`
NewYork NewYork `json:"new-york"`
Debate Debate `json:"debate"`
SeanKelly SeanKelly `json:"sean-kelly"`
MarkBates MarkBates `json:"mark-bates"`
Denver Denver `json:"denver"`
Home Home `json:"home"`
}
type StoryLine map[string]Section

type Options struct {
Text string `json:"text"`
Arc string `json:"arc"`
}

type Intro struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []Options `json:"options"`
}

type NewYork struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []Options `json:"options"`
}

type Debate struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []Options `json:"options"`
}

type SeanKelly struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []Options `json:"options"`
}

type MarkBates struct {
type Section struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []Options `json:"options"`
}

type Denver struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []Options `json:"options"`
}

type Home struct {
Title string `json:"title"`
Story []string `json:"story"`
Options []any `json:"options"`
}
19 changes: 10 additions & 9 deletions cyoa/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"html/template"
"net/http"
"os"
"reflect"
"strings"
)

Expand All @@ -19,6 +18,10 @@ func main() {
func handlerHttp(w http.ResponseWriter, r *http.Request) {

pathSegments := strings.Split(r.URL.Path, "/")
if r.Method != "GET" {
fmt.Println("Method not allowed")
return
}
expectedArc := pathSegments[1]

jsonInput, err := os.ReadFile("data.json")
Expand All @@ -35,20 +38,18 @@ func handlerHttp(w http.ResponseWriter, r *http.Request) {

t, _ := template.ParseFiles("template.html")

val := reflect.ValueOf(story)
typ := reflect.TypeOf(story)
var retVal string
retVal, ok := story[expectedArc]

for i := 0; i < val.NumField(); i++ {
if typ.Field(i).Name == expectedArc {
fmt.Println(val.Field(i))
retVal = val.Field(i).String()
}
if !ok {
fmt.Printf("path %s not found in the data source\n", expectedArc)
return
}

err = t.Execute(w, retVal)

if err != nil {
fmt.Println("whaa")
fmt.Println(err)
panic(err)
}
}
21 changes: 20 additions & 1 deletion cyoa/main/template.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
<html>

<body>
<h1> {{ . }} </h1>

<h1> {{ .Title }} </h1>
<div class="our-story">
{{range $idx, $para := .Story}}
<div>
<p>{{$para}}</p>
</div>
{{end}}
</div>
{{ if .Options }}
<h2>Go to:</h2>
{{ end}}
{{range $idx, $option := .Options}}
<div>
<p>{{$option.Text}}</p>
<a href="{{$option.Arc}}">{{$option.Arc}}</a>
</div>
{{end}}


</body>

</html>

0 comments on commit 9b88bf4

Please sign in to comment.