From a1edb7fa63dd290654884eeabf89acc9e5965ae5 Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Tue, 5 Mar 2024 20:09:38 +0100
Subject: model: Move timestamp updates to controller

The model package should handle the object relational mapping (ORM).
This requires implementing the four CRUD methods:

- create
- read
- update
- delete

On create and update the model package used to modify the timestamps
like `last_changed`. This was detected by the unit tests which tested
that the data is not changed in a create / read cycle or is updated
correctly in an update / read cycle.

This raised the question if it is a good idea that the model package is
"smart" and updates timestamps. To keep the model package and the
included unit tests simple the new design enforces that the complete
data - including metadata - is always exactly the same after using any
CRUD methods.

The functionality of updating the timestamp is moved to the HTTP handler
inside the controller package. This also matches the definition of the
controller package as the part of the code which is alone responsible to
actually change the data.

This commit finally fixes the unit test suite.
---
 controller/recipe.go | 4 ++++
 1 file changed, 4 insertions(+)

(limited to 'controller/recipe.go')

diff --git a/controller/recipe.go b/controller/recipe.go
index 818a077..6acb062 100644
--- a/controller/recipe.go
+++ b/controller/recipe.go
@@ -13,6 +13,8 @@ import (
 func RecipeCreate(w http.ResponseWriter, r *http.Request) {
 	recipe := model.Recipe{}
 	recipe.Title = "recipe without title"
+	recipe.Touch()
+	recipe.Created = recipe.LastChanged
 
 	err := recipe.Create()
 	if err != nil {
@@ -42,6 +44,8 @@ func RecipeUpdate(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	recipe.Touch()
+
 	err = recipe.Update()
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
-- 
cgit v1.2.3-70-g09d2