Skip to content

Commit

Permalink
Update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abneed committed May 22, 2022
1 parent cad6d42 commit 7743fee
Show file tree
Hide file tree
Showing 27 changed files with 1,267 additions and 102 deletions.
39 changes: 27 additions & 12 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"encoding/gob"
"fmt"
"log"
"net/http"
"time"

"github.com/abneed/bookings/pkg/config"
"github.com/abneed/bookings/pkg/handlers"
"github.com/abneed/bookings/pkg/render"
"github.com/abneed/bookings/internal/config"
"github.com/abneed/bookings/internal/handlers"
"github.com/abneed/bookings/internal/models"
"github.com/abneed/bookings/internal/render"
"github.com/alexedwards/scs/v2"
)

Expand All @@ -19,6 +21,26 @@ var session *scs.SessionManager

// main is the main application function
func main() {
err := run()
if err != nil {
log.Fatal(err)
}

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
// _ = http.ListenAndServe(portNumber, nil)

srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
srv.ListenAndServe()
log.Fatal(err)
}

func run() error {
// what am I going to put in the session
gob.Register(models.Reservation{})

// change this to true when in production
app.InProduction = false

Expand All @@ -33,6 +55,7 @@ func main() {
tc, err := render.CreateTemplateCache()
if err != nil {
log.Fatal("cannot create template cache")
return err
}

app.TemplateCache = tc
Expand All @@ -42,13 +65,5 @@ func main() {
handlers.NewHandlers(repo)
render.NewTemplates(&app)

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
// _ = http.ListenAndServe(portNumber, nil)

srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
srv.ListenAndServe()
log.Fatal(err)
return nil
}
10 changes: 10 additions & 0 deletions cmd/web/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "testing"

func TestRun(t *testing.T) {
err := run()
if err != nil {
t.Error("failed run()")
}
}
33 changes: 33 additions & 0 deletions cmd/web/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"net/http"
"testing"
)

func TestNoSurf(t *testing.T) {
var myH myHandler

h := NoSurf(&myH)

switch v := h.(type) {
case http.Handler:
// do nothing
default:
t.Error(fmt.Sprintf("type is not http.Handler, but is %T", v))
}
}

func TestSessionLoad(t *testing.T) {
var myH myHandler

h := SessionLoad(&myH)

switch v := h.(type) {
case http.Handler:
// do nothing
default:
t.Error(fmt.Sprintf("type is not http.Handler, but is %T", v))
}
}
16 changes: 14 additions & 2 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"net/http"

"github.com/abneed/bookings/pkg/config"
"github.com/abneed/bookings/pkg/handlers"
"github.com/abneed/bookings/internal/config"
"github.com/abneed/bookings/internal/handlers"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
Expand All @@ -18,6 +18,18 @@ func routes(app *config.AppConfig) http.Handler {

mux.Get("/", handlers.Repo.Home)
mux.Get("/about", handlers.Repo.About)
mux.Get("/generals-quarters", handlers.Repo.Generals)
mux.Get("/majors-suite", handlers.Repo.Majors)

mux.Get("/search-availability", handlers.Repo.Availability)
mux.Post("/search-availability", handlers.Repo.PostAvailability)
mux.Post("/search-availability-json", handlers.Repo.AvailabilityJSON)

mux.Get("/contact", handlers.Repo.Contact)

mux.Get("/make-reservation", handlers.Repo.Reservation)
mux.Post("/make-reservation", handlers.Repo.PostReservation)
mux.Get("/reservation-summary", handlers.Repo.ReservationSummary)

fileServer := http.FileServer(http.Dir("./static/"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
Expand Down
22 changes: 22 additions & 0 deletions cmd/web/routes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"testing"

"github.com/abneed/bookings/internal/config"
"github.com/go-chi/chi"
)

func TestRoutes(t *testing.T) {
var app config.AppConfig

mux := routes(&app)

switch v := mux.(type) {
case *chi.Mux:
// do nothing; test passed
default:
t.Error(fmt.Sprintf("type is not *chi.Mux, type is %T", v))
}
}
18 changes: 18 additions & 0 deletions cmd/web/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"net/http"
"os"
"testing"
)

func TestMain(m *testing.M) {

os.Exit(m.Run())
}

type myHandler struct{}

func (mh *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

}
File renamed without changes.
18 changes: 18 additions & 0 deletions internal/forms/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package forms

type errors map[string][]string

// Add adds an error message for a given form field
func (e errors) Add(field, message string) {
e[field] = append(e[field], message)
}

// Get returns the first error message
func (e errors) Get(field string) string {
es := e[field]
if len(es) == 0 {
return ""
}

return es[0]
}
66 changes: 66 additions & 0 deletions internal/forms/forms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package forms

import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/asaskevich/govalidator"
)

// Form creates a custom form struct, embeds a url.Values object
type Form struct {
url.Values
Errors errors
}

// Valid returns true if there are no errors, otherwise false
func (f *Form) Valid() bool {
return len(f.Errors) == 0
}

// New initializes a form struct
func New(data url.Values) *Form {
return &Form{
data,
errors(map[string][]string{}),
}
}

// Required checks for required fields
func (f *Form) Required(fields ...string) {
for _, field := range fields {
value := f.Get(field)
if strings.TrimSpace(value) == "" {
f.Errors.Add(field, "This field cannot be blank")
}
}
}

// Has checks if form field is in post and not empty
func (f *Form) Has(field string, r *http.Request) bool {
x := r.Form.Get(field)
if x == "" {

return false
}
return true
}

// MinLength checks for strign minimum length
func (f *Form) MinLength(field string, length int, r *http.Request) bool {
x := r.Form.Get(field)
if len(x) < length {
f.Errors.Add(field, fmt.Sprintf("This field must be at least %d characters long", length))
return false
}
return true
}

// IsEmail checks for valid email address
func (f *Form) IsEmail(field string) {
if !govalidator.IsEmail(f.Get(field)) {
f.Errors.Add(field, "Invalid email address")
}
}
Loading

0 comments on commit 7743fee

Please sign in to comment.