This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
controllers.controller('BlogListCtrl', function ($scope, PostService) { | |
$scope.posts = PostService.query(); | |
}); | |
controllers.controller('BlogDetailCtrl', function ($scope, $routeParams, PostService) { | |
$scope.post = PostService.get({id: $routeParams.id}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
controllers.controller('PostEditCtrl', function ($scope, $routeParams, $location, PostService, toastr) { | |
$scope.post = PostService.get({id: $routeParams.id}); | |
$scope.save = function () { | |
PostService.update({id: $routeParams.id}, $scope.post) | |
.$promise | |
.then(function () { | |
toastr.success('Post salvo com sucesso'); | |
$location.path('#/list'); | |
}, function () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
controllers.controller('PostCreateCtrl', function ($scope, $location, PostService, toastr) { | |
$scope.save = function () { | |
PostService.save($scope.post) | |
.$promise | |
.then(function () { | |
toastr.success('Post salvo com sucesso'); | |
$location.path('#/list'); | |
}, function () { | |
toastr.error('Ocorreu um erro ao tentar salvar o post'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var controllers = angular.module('ninja.controllers', []); | |
controllers.controller('PostListCtrl', function ($scope, PostService, toastr) { | |
$scope.posts = PostService.query(); | |
$scope.delete = function (id) { | |
PostService.delete({id: id}) | |
.$promise |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var services = angular.module('ninja.services', []); | |
services.factory('PostService', function ($resource, API) { | |
return $resource(API + '/posts/:id', {id: '@id'}, { | |
update:{ | |
method:'PUT' | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var app = angular.module('ninja', ['ngRoute', 'ngResource', 'ngAnimate', 'ninja.services', 'ninja.controllers', 'toastr']); | |
app.constant('API', 'api.php'); | |
app.config(function ($routeProvider) { | |
$routeProvider.when('/', { | |
controller: 'BlogListCtrl', | |
templateUrl: 'view/blog/list.html' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="pt-br" ng-app="ninja"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Blog Ninja</title> | |
<!-- css de terceiros --> | |
<link href="assets/vendor/bower/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app->delete('/posts/{id}', function ($id) use ($app) { | |
$em = $app['orm.em']; | |
$post = $em->getRepository('App\Entities\Post')->find($id); | |
$em->remove($post); | |
$em->flush(); | |
return $app->json(true); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app->put('/posts/{id}', function ($id, Request $request) use ($app) { | |
$em = $app['orm.em']; | |
$post = $em->getRepository('App\Entities\Post')->find($id); | |
$post->setTitle($request->get('title')); | |
$post->setContent($request->get('content')); | |
$em->persist($post); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app->post('/posts', function (Request $request) use ($app) { | |
$post = new Post(); | |
$post->setTitle($request->get('title')); | |
$post->setContent($request->get('content')); | |
$em = $app['orm.em']; | |
$em->persist($post); |