I have two objects, Events & Comments:
{
"name": "events",
"fields": {
"name": {
"type": "string"
},
"date": {
"type": "datetime"
},
"time": {
"type": "datetime"
},
"info": {
"type": "text"
},
"users": {
"collection": "users_events",
"via": "event"
},
"eventCommentsId": {
"collection": "comments",
"via": "eventId"
},
}
},
{
"name": "comments",
"fields": {
"content": {
"type": "text"
},
"owner": {
"object": "users"
},
"eventId": {
"object": "events"
},
"date": {
"type": "datetime"
}
}
}
Each event should have its own unique collection of comments. So, its a One to Many relationship.
Right now, I can just get all of the comments instead of only the ones that correspond to each event. My thinking is that I need to include the id of the event in each comment. But, I'm not entirely sure how to do that.
If anyone could help me out with this, that would be amazing!
I'm building the app with Ionic/AngularJS and I'm storing my data with Backand.
Thanks in advance!
.controller('EventDetailCtrl', ['$scope', '$stateParams', '$ionicSideMenuDelegate', 'EventService', 'CommentService',function($scope, $stateParams, $ionicSideMenuDelegate, EventService, CommentService) {
$scope.openMenu = function () {
$ionicSideMenuDelegate.toggleLeft();
};
var id = $stateParams.id;
EventService.getEvent(id).then(function(response){
$scope.event = response.data;
});
$scope.comments = [];
$scope.input = {};
function getAllComments() {
CommentService.getComments()
.then(function (result) {
$scope.comments = result.data.data;
});
}
$scope.addComment = function() {
CommentService.addComment($scope.input)
.then(function(result) {
$scope.input = {};
getAllComments();
});
}
$scope.deleteComment = function(id) {
CommentService.deleteComment(id)
.then(function (result) {
getAllComments();
});
}
getAllComments();
}])
.service('CommentService', function ($http, Backand) {
var baseUrl = '/1/objects/';
var objectName = 'comments/';
function getUrl() {
return Backand.getApiUrl() + baseUrl + objectName;
}
function getUrlForId(id) {
return getUrl() + id;
}
getComments = function () {
return $http.get(getUrl());
};
addComment = function(event) {
return $http.post(getUrl(), event);
}
deleteComment = function (id) {
return $http.delete(getUrlForId(id));
};
getComment = function (id) {
return $http.get(getUrlForId(id));
};
return {
getComments: getComments,
addComment: addComment,
deleteComment: deleteComment,
getComment: getComment
}
})