From 61ed59949d1af8100eedf9f7a737a7261138e6b3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Pailharey?= <rpailharey@grandlyon.com>
Date: Thu, 15 Jul 2021 10:27:52 +0200
Subject: [PATCH] feat: Notify when new post created + handle errors

---
 internal/backoffice/backoffice.go | 10 +++++++++-
 internal/post/post.go             |  7 +++++--
 template.env                      | 16 ++++++++++++++++
 web/components/post/post.js       |  3 +++
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/internal/backoffice/backoffice.go b/internal/backoffice/backoffice.go
index f89523a..135175d 100644
--- a/internal/backoffice/backoffice.go
+++ b/internal/backoffice/backoffice.go
@@ -2,6 +2,7 @@ package backoffice
 
 import (
 	"io/ioutil"
+	"log"
 	"net/http"
 
 	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/post"
@@ -18,5 +19,12 @@ func HandleCreatePost(w http.ResponseWriter, r *http.Request) {
 		w.WriteHeader(http.StatusInternalServerError)
 		return
 	}
-	post.Create(string(body))
+
+	err = post.Create(string(body))
+	if err != nil {
+		w.WriteHeader(http.StatusInternalServerError)
+		return
+	}
+
+	log.Printf("| New post | %v", r.RemoteAddr)
 }
diff --git a/internal/post/post.go b/internal/post/post.go
index f39bfe3..79a0d7a 100644
--- a/internal/post/post.go
+++ b/internal/post/post.go
@@ -39,6 +39,9 @@ func Exists(content string) bool {
 	return true
 }
 
-func Create(content string) {
-	db.Create(&Post{Content: content})
+func Create(content string) error {
+	if err := db.Create(&Post{Content: content}).Error; err != nil {
+		return err
+	}
+	return nil
 }
diff --git a/template.env b/template.env
index 8e59d8d..239c9cc 100644
--- a/template.env
+++ b/template.env
@@ -1,3 +1,19 @@
+# Common settings
+HOSTNAME=
+ADMIN_ROLE=
+DEBUG_MODE=
+HTTPS_PORT=
+
+# Needed to user OAuth2 authentication :
+REDIRECT_URL=
+CLIENT_ID=
+CLIENT_SECRET=
+AUTH_URL=
+TOKEN_URL=
+USERINFO_URL=
+LOGOUT_URL=
+
+# Access to the database
 DATABASE_USER=
 DATABASE_PASSWORD=
 DATABASE_NAME=
\ No newline at end of file
diff --git a/web/components/post/post.js b/web/components/post/post.js
index 58061be..b99a1fa 100644
--- a/web/components/post/post.js
+++ b/web/components/post/post.js
@@ -1,5 +1,6 @@
 // Imports
 import { HandleError } from "/services/common/errors.js";
+import * as Messages from "/services/messages/messages.js";
 
 export async function mount(where, user) {
   const postComponent = new Post(user);
@@ -53,6 +54,8 @@ class Post {
           `Le post n'a pas pu être créé (code ${response.status})`
         );
       }
+      Messages.Show("is-success", "Le post a été créé avec succès");
+
     } catch (e) {
       HandleError(e);
       this.post_textarea.value = "Une erreur s'est produite !";
-- 
GitLab