From f917eedace32edefd4ee3e91ac52d0021ca70c74 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 3 Feb 2018 18:42:27 +0100 Subject: [PATCH] upload thumb image of the article, some stuff fixed --- RESTfunctions.go | 14 ++ webapp/app.js | 5 +- webapp/views/main/main.html | 10 +- webapp/views/main/main.js | 2 +- webapp/views/post/post.html | 10 +- .../views/templates/post-thumb-template.html | 31 ++-- .../views/templates/user-thumb-template.html | 2 +- webapp/views/user/user.html | 6 +- webapp/views/write/write.html | 5 + webapp/views/write/write.js | 157 +++++++++++------- 10 files changed, 158 insertions(+), 84 deletions(-) diff --git a/RESTfunctions.go b/RESTfunctions.go index 9fd9afc..8eaa70e 100644 --- a/RESTfunctions.go +++ b/RESTfunctions.go @@ -138,6 +138,13 @@ func GetUser(w http.ResponseWriter, r *http.Request) { err = postCollection.Find(bson.M{"user._id": bson.ObjectIdHex(userid)}).Limit(50).All(&user.Posts) check(err) + //for each post, get the user + for i, _ := range user.Posts { + err = userCollection.Find(bson.M{"_id": user.Posts[i].User.ID}).One(&user.Posts[i].User) + check(err) + //TODO don't return the user.Token, password, etc only the Name, LastName, Username, img + } + jResp, err := json.Marshal(user) check(err) fmt.Fprintln(w, string(jResp)) @@ -198,6 +205,13 @@ func GetPosts(w http.ResponseWriter, r *http.Request) { err := postCollection.Find(bson.M{}).Limit(50).All(&posts) check(err) + //for each post, get the user + for i, _ := range posts { + fmt.Println(posts[i].User.ID) + err = userCollection.Find(bson.M{"_id": posts[i].User.ID}).One(&posts[i].User) + check(err) + //TODO don't return the user.Token, password, etc only the Name, LastName, Username, img + } jResp, err := json.Marshal(posts) check(err) fmt.Fprintln(w, string(jResp)) diff --git a/webapp/app.js b/webapp/app.js index 67db7a5..5b08b17 100644 --- a/webapp/app.js +++ b/webapp/app.js @@ -1,6 +1,7 @@ 'use strict'; var apiurl = "http://127.0.0.1:3000/"; +var ipfsurl = "http://127.0.0.1:5001/api/v0/"; angular.module('app', [ 'ngRoute', @@ -77,12 +78,12 @@ console.log("window", window.location.hash); .factory('api', function($http) { return { init: function() { - console.log("http", $http.options); + /*console.log("http", $http.options); var dblog_user = JSON.parse(localStorage.getItem('dblog_user')); if (dblog_user) { $http.defaults.headers.common['Authorization'] = dblog_user.token; $http.defaults.headers.post['Authorization'] = dblog_user.token; - } + }*/ } }; }) diff --git a/webapp/views/main/main.html b/webapp/views/main/main.html index b754ddd..929d7f8 100755 --- a/webapp/views/main/main.html +++ b/webapp/views/main/main.html @@ -1,7 +1,7 @@
-
+
@@ -9,7 +9,7 @@
-
+
@@ -17,7 +17,7 @@
-
+
@@ -25,7 +25,7 @@
-
+
diff --git a/webapp/views/main/main.js b/webapp/views/main/main.js index d36b0e4..ab464c7 100755 --- a/webapp/views/main/main.js +++ b/webapp/views/main/main.js @@ -12,7 +12,7 @@ angular.module('app.main', ['ngRoute']) .controller('MainCtrl', function($scope, $rootScope, $http) { - $scope.posts = {}; + $scope.posts = []; $http.get(apiurl + 'posts') .then(function(data) { console.log('data success'); diff --git a/webapp/views/post/post.html b/webapp/views/post/post.html index d045071..4fcc8a9 100644 --- a/webapp/views/post/post.html +++ b/webapp/views/post/post.html @@ -30,9 +30,13 @@
-
{{post.title}}
-
{{post.summary}}
- +
+
{{post.title}}
+
{{post.summary}}
+
+ +
+
diff --git a/webapp/views/templates/post-thumb-template.html b/webapp/views/templates/post-thumb-template.html index dc8434d..b6d563e 100644 --- a/webapp/views/templates/post-thumb-template.html +++ b/webapp/views/templates/post-thumb-template.html @@ -1,23 +1,34 @@
+
+
+ +
+
diff --git a/webapp/views/write/write.js b/webapp/views/write/write.js index e29199f..19ed35f 100644 --- a/webapp/views/write/write.js +++ b/webapp/views/write/write.js @@ -2,66 +2,105 @@ angular.module('app.write', ['ngRoute']) - .config(['$routeProvider', function($routeProvider) { - $routeProvider.when('/write', { - templateUrl: 'views/write/write.html', - controller: 'WriteCtrl' - }); - }]) + .config(['$routeProvider', function($routeProvider) { + $routeProvider.when('/write', { + templateUrl: 'views/write/write.html', + controller: 'WriteCtrl' + }); + }]) - .controller('WriteCtrl', function($scope, $rootScope, $http) { - $scope.post = { - title: "", - content: "", - summary: "" - }; - var editor = new MediumEditor('.editable', { - toolbar: { - /* These are the default options for the toolbar, - if nothing is passed this is what is used */ - allowMultiParagraphSelection: true, - buttons: ['bold', 'italic', 'underline', 'anchor', 'h2', 'h3', 'quote'], - diffLeft: 0, - diffTop: -10, - firstButtonClass: 'medium-editor-button-first', - lastButtonClass: 'medium-editor-button-last', - relativeContainer: null, - standardizeSelectionStart: false, - static: false, - /* options which only apply when static is true */ - align: 'center', - sticky: false, - updateOnEmptySelection: false - }, - autoLink: true, - placeholder: { - text: 'Start to writting your decentralized blog post...' - } - }).subscribe('editableInput', function (event, editable) { - // Do some work - $scope.post.content = editable.innerHTML; - console.log($scope.post.content); - }); + .controller('WriteCtrl', function($scope, $rootScope, $http, toastr) { + $scope.post = { + title: "", + content: "", + summary: "" + }; + var editor = new MediumEditor('.editable', { + toolbar: { + /* These are the default options for the toolbar, + if nothing is passed this is what is used */ + allowMultiParagraphSelection: true, + buttons: ['bold', 'italic', 'underline', 'anchor', 'h2', 'h3', 'quote'], + diffLeft: 0, + diffTop: -10, + firstButtonClass: 'medium-editor-button-first', + lastButtonClass: 'medium-editor-button-last', + relativeContainer: null, + standardizeSelectionStart: false, + static: false, + /* options which only apply when static is true */ + align: 'center', + sticky: false, + updateOnEmptySelection: false + }, + autoLink: true, + placeholder: { + text: 'Start to writting your decentralized blog post...' + } + }).subscribe('editableInput', function(event, editable) { + // Do some work + $scope.post.content = editable.innerHTML; + }); - $scope.publicate = function() { - console.log("post", $scope.post); - $http({ - url: apiurl + 'post', - method: "POST", - headers: { - "Content-Type": undefined - }, - data: $scope.post - }) - .then(function(data) { - console.log("data: "); - console.log(data.data); - window.location = "#!/user/" + $scope.user.id; - }, - function(data) { - console.log(data); - }); + $scope.publicate = function() { + //add the thumbimg to ipfs + var formdata = new FormData(); + formdata.append("file", $scope.file); + $http({ + url: ipfsurl + 'add', + method: "POST", + headers: { + "Content-Type": undefined + }, + data: formdata + }) + .then(function(data) { + console.log("data: "); + console.log(data.data); + $scope.post.thumbimg = data.data.Hash; + toastr.success("Thumb image added to IPFS"); + $scope.publicate2(); + }, + function(data) { + console.log(data); + toastr.error("Error adding Model to IPFS"); + }); + }; + $scope.publicate2 = function() { + //$scope.post.thumbimg = hash; + //send the post content to the Go api, that will add the html content to ipfs + console.log("post", $scope.post); + $http({ + url: apiurl + 'post', + method: "POST", + headers: { + "Content-Type": undefined + }, + data: $scope.post + }) + .then(function(data) { + console.log("data: "); + console.log(data.data); + window.location = "#!/main"; + }, + function(data) { + console.log(data); + }); - }; - }); + }; + }).directive('fileModel', ['$parse', function($parse) { + //directive code from https://www.tutorialspoint.com/angularjs/angularjs_upload_file.htm + return { + restrict: 'A', + link: function(scope, element, attrs) { + var model = $parse(attrs.fileModel); + var modelSetter = model.assign; + element.bind('change', function() { + scope.$apply(function() { + modelSetter(scope, element[0].files[0]); + }); + }); + } + }; + }]);