Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ linters:
- sqlclosecheck
- rowserrcheck
settings:
cyclop:
max-complexity: 13
gosec:
excludes:
- G104 # Unhandled errors (errcheck handles this better)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ confirm:
## run/web: run the web application with live reload
.PHONY: run/web
run/web:
reflex -s -r '\.(go|tmpl|js)$$' -- go run -tags sqlite ./cmd/web serve
reflex -s -r '\.(go|tmpl|js)$$' -- go run -tags sqlite ./cmd/web serve --max-orgs=2

## run/www: run the web application with live reload
.PHONY: run/www
Expand Down
4 changes: 4 additions & 0 deletions cmd/web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (app *application) getNotFound(w http.ResponseWriter, r *http.Request) *htt
return app.html.Render(w, r, http.StatusOK, pages.NotFound, app.html.TemplateData(r))
}

func (app *application) getForbidden(w http.ResponseWriter, r *http.Request) *httperr.Error {
return app.html.Render(w, r, http.StatusOK, pages.Forbidden, app.html.TemplateData(r))
}

func (app *application) GetHealthz(_ context.Context) (*gen.HealthzResponse, error) {
return &gen.HealthzResponse{
Status: "ok",
Expand Down
23 changes: 19 additions & 4 deletions cmd/web/handlers:equipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type equipmentData struct {

type equipmentPrintData struct {
OrgID string
OrgName string
OrgDisplayName string
Inventories []equipment.Equipment
Query string
Category string
Expand Down Expand Up @@ -301,7 +301,8 @@ func (app *application) getEquipmentItem(w http.ResponseWriter, r *http.Request)
app.resolveItemURLs(item)

data := app.html.TemplateData(r)
data.Form = &equipment.DetailsForm{}
f := equipment.DetailsFormFromEquipment(item)
data.Form = &f
data.Data = equipmentItemData{OrgID: orgID, Item: item, ID: itemID, ActiveTab: "details"}
return app.html.Render(w, r, http.StatusOK, pages.EquipmentDetail, data)
}
Expand Down Expand Up @@ -363,6 +364,20 @@ func (app *application) postEquipmentItemProperties(w http.ResponseWriter, r *ht
return &httperr.Error{Error: err, Message: "Bad request.", Code: http.StatusBadRequest}
}

if !form.Validate() {
item, err := app.services.equipment.GetByID(ctx, itemID)
if err != nil {
return &httperr.Error{Error: err, Message: "Failed to retrieve inventory item.", Code: http.StatusInternalServerError}
}

app.resolveItemURLs(item)

data := app.html.TemplateData(r)
data.Form = &form
data.Data = equipmentItemData{OrgID: orgID, Item: item, ID: itemID, ActiveTab: "properties"}
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.EquipmentProperties, data)
}

if err := app.services.equipment.UpdateProperties(ctx, equipment.UpdateEquipmentProperties{
ID: itemID,
Properties: form.ToProperties(),
Expand Down Expand Up @@ -974,7 +989,7 @@ func (app *application) getEquipmentPrint(w http.ResponseWriter, r *http.Request
ctx := r.Context()
orgID := r.PathValue("org_id")

org, err := app.services.orgs.GetByID(ctx, orgID)
org, err := app.services.orgs.Get(ctx, orgID)
if err != nil {
return &httperr.Error{Error: err, Message: "Failed to retrieve organization.", Code: http.StatusInternalServerError}
}
Expand Down Expand Up @@ -1006,7 +1021,7 @@ func (app *application) getEquipmentPrint(w http.ResponseWriter, r *http.Request
data := app.html.TemplateData(r)
data.Data = equipmentPrintData{
OrgID: orgID,
OrgName: org.Name,
OrgDisplayName: org.DisplayName,
Inventories: filtered,
Query: query,
Category: category,
Expand Down
23 changes: 7 additions & 16 deletions cmd/web/handlers:equipment:categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ type equipmentCategoriesData struct {
}

type equipmentCategoryData struct {
OrgID string
Category *categories.EquipmentCategory
ID string
OrgID string
ID string
}

func (app *application) getEquipmentCategories(w http.ResponseWriter, r *http.Request) *httperr.Error {
Expand Down Expand Up @@ -124,8 +123,8 @@ func (app *application) getEquipmentCategory(w http.ResponseWriter, r *http.Requ
}

data := app.html.TemplateData(r)
data.Form = &categories.Form{}
data.Data = equipmentCategoryData{OrgID: orgID, Category: category, ID: catID}
data.Form = &categories.Form{Name: category.Name}
data.Data = equipmentCategoryData{OrgID: orgID, ID: catID}
return app.html.Render(w, r, http.StatusOK, pages.EquipmentCategoriesDetail, data)
}

Expand All @@ -139,18 +138,10 @@ func (app *application) postEquipmentCategory(w http.ResponseWriter, r *http.Req
return &httperr.Error{Error: err, Message: "Bad request.", Code: http.StatusBadRequest}
}

category, err := app.services.equipmentcategories.GetByID(ctx, catID)
if err != nil {
if errors.Is(err, database.ErrNotFound) {
return &httperr.Error{Error: err, Message: "Equipment category not found.", Code: http.StatusNotFound}
}
return &httperr.Error{Error: err, Message: "Failed to retrieve equipment category.", Code: http.StatusInternalServerError}
}

reRender := func(f *categories.Form) *httperr.Error {
data := app.html.TemplateData(r)
data.Form = f
data.Data = equipmentCategoryData{OrgID: orgID, Category: category, ID: catID}
data.Data = equipmentCategoryData{OrgID: orgID, ID: catID}
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.EquipmentCategoriesDetail, data)
}

Expand Down Expand Up @@ -192,11 +183,11 @@ func (app *application) postDeleteEquipmentCategory(w http.ResponseWriter, r *ht
}
return &httperr.Error{Error: fetchErr, Message: "Failed to retrieve equipment category.", Code: http.StatusInternalServerError}
}
f := &categories.Form{}
f := &categories.Form{Name: category.Name}
f.AddError("delete", "Cannot delete: this category is assigned to one or more equipment items.")
data := app.html.TemplateData(r)
data.Form = f
data.Data = equipmentCategoryData{OrgID: orgID, Category: category, ID: catID}
data.Data = equipmentCategoryData{OrgID: orgID, ID: catID}
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.EquipmentCategoriesDetail, data)
}
return &httperr.Error{
Expand Down
11 changes: 5 additions & 6 deletions cmd/web/handlers:equipment:locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ type locationsData struct {
}

type locationData struct {
OrgID string
Location *locations.Location
ID string
OrgID string
ID string
}

func (app *application) getLocations(w http.ResponseWriter, r *http.Request) *httperr.Error {
Expand Down Expand Up @@ -125,8 +124,8 @@ func (app *application) getLocation(w http.ResponseWriter, r *http.Request) *htt
}

data := app.html.TemplateData(r)
data.Form = &locations.Form{}
data.Data = locationData{OrgID: orgID, Location: loc, ID: locID}
data.Form = &locations.Form{Name: loc.Name}
data.Data = locationData{OrgID: orgID, ID: locID}
return app.html.Render(w, r, http.StatusOK, pages.LocationsDetail, data)
}

Expand All @@ -150,7 +149,7 @@ func (app *application) postLocation(w http.ResponseWriter, r *http.Request) *ht
reRender := func(f *locations.Form) *httperr.Error {
data := app.html.TemplateData(r)
data.Form = f
data.Data = locationData{OrgID: loc.OrgID, Location: loc, ID: locID}
data.Data = locationData{OrgID: loc.OrgID, ID: locID}
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.LocationsDetail, data)
}

Expand Down
15 changes: 7 additions & 8 deletions cmd/web/handlers:equipment:manufacturers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ type manufacturersData struct {
}

type manufacturerData struct {
OrgID string
Manufacturer *manufacturers.Manufacturer
ID string
OrgID string
ID string
}

func (app *application) getManufacturers(w http.ResponseWriter, r *http.Request) *httperr.Error {
Expand Down Expand Up @@ -125,8 +124,8 @@ func (app *application) getManufacturer(w http.ResponseWriter, r *http.Request)
}

data := app.html.TemplateData(r)
data.Form = &manufacturers.Form{}
data.Data = manufacturerData{OrgID: orgID, Manufacturer: manufacturer, ID: mfrID}
data.Form = &manufacturers.Form{Name: manufacturer.Name}
data.Data = manufacturerData{OrgID: orgID, ID: mfrID}
return app.html.Render(w, r, http.StatusOK, pages.ManufacturersDetail, data)
}

Expand All @@ -150,7 +149,7 @@ func (app *application) postManufacturer(w http.ResponseWriter, r *http.Request)
reRender := func(f *manufacturers.Form) *httperr.Error {
data := app.html.TemplateData(r)
data.Form = f
data.Data = manufacturerData{OrgID: mfr.OrgID, Manufacturer: mfr, ID: mfrID}
data.Data = manufacturerData{OrgID: mfr.OrgID, ID: mfrID}
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.ManufacturersDetail, data)
}

Expand Down Expand Up @@ -193,11 +192,11 @@ func (app *application) postDeleteManufacturer(w http.ResponseWriter, r *http.Re
}
return &httperr.Error{Error: fetchErr, Message: "Failed to retrieve manufacturer.", Code: http.StatusInternalServerError}
}
f := &manufacturers.Form{}
f := &manufacturers.Form{Name: manufacturer.Name}
f.AddError("delete", "Cannot delete: this manufacturer is assigned to one or more inventory items.")
data := app.html.TemplateData(r)
data.Form = f
data.Data = manufacturerData{OrgID: orgID, Manufacturer: manufacturer, ID: mfrID}
data.Data = manufacturerData{OrgID: orgID, ID: mfrID}
return app.html.Render(w, r, http.StatusUnprocessableEntity, pages.ManufacturersDetail, data)
}
return &httperr.Error{
Expand Down
Loading