Skip to content

Commit 812a8a3

Browse files
committed
Generate create note
1 parent 37b7705 commit 812a8a3

File tree

11 files changed

+100
-36
lines changed

11 files changed

+100
-36
lines changed

app/Http/Controllers/NoteController.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Note;
6+
use App\User;
67
use Illuminate\Http\Request;
78

89
class NoteController extends Controller
@@ -24,7 +25,7 @@ public function __construct(Note $note)
2425
*/
2526
public function index()
2627
{
27-
return $this->note->with('user')->get();
28+
return $this->note->with('user')->latest()->get();
2829
}
2930

3031
/**
@@ -40,13 +41,23 @@ public function create()
4041
/**
4142
* Store a newly created resource in storage.
4243
*
43-
* @param \Illuminate\Http\Request $request
44+
* @param \Illuminate\Http\Request $request
45+
* @param User $user
4446
* @return \Illuminate\Http\Response
4547
*/
46-
public function store(Request $request)
48+
public function store(Request $request, User $user)
4749
{
48-
$this->note->create($request->all());
49-
50+
//Validation
51+
$this->validate($request, [
52+
'name' => 'required',
53+
'description' => 'required|min:10'
54+
]);
55+
//Store note
56+
$this->note->user_id = $user->inRandomOrder()->value('id');
57+
$this->note->name = $request->name;
58+
$this->note->description = $request->description;
59+
$this->note->save();
60+
//Response
5061
return response()->json([
5162
'message' => 'Note Created.'
5263
]);

public/js/app.ba5a7c203864864cd0f7.js renamed to public/js/app.526e6b4882a4fcd54afe.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mix-manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"/js/app.js": "/js/app.ba5a7c203864864cd0f7.js",
3-
"/css/app.css": "/css/app.75af5814e06517a0a5bb.css"
2+
"/js/app.js": "/js/app.526e6b4882a4fcd54afe.js",
3+
"/css/app.css": "/css/app.8a420a94e364c9c91fce.css"
44
}

resources/assets/js/app.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11

2-
/**
3-
* First we will load all of this project's JavaScript dependencies which
4-
* includes Vue and other libraries. It is a great starting point when
5-
* building robust, powerful web applications using Vue and Laravel.
6-
*/
7-
82
import './bootstrap';
93

104
import router from './routes';
115

12-
/**
13-
* Next, we will create a fresh Vue application instance and attach it to
14-
* the page. Then, you may begin adding components to this application
15-
* or customize the JavaScript scaffolding to fit your unique needs.
16-
*/
6+
import './components';
177

188
const app = new Vue({
199
el: '#app',

resources/assets/js/bootstrap.js

-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import loadash from 'lodash';
22
window._ = loadash;
33

4-
/**
5-
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
6-
* for JavaScript based Bootstrap features such as modals and tabs. This
7-
* code may be modified to fit the specific needs of your application.
8-
*/
94
import jQuery from 'jquery';
105

116
window.$ = window.jQuery = jQuery;
@@ -26,12 +21,6 @@ window.Vue = Vue;
2621

2722
Vue.use(VueRouter);
2823

29-
/**
30-
* We'll load the axios HTTP library which allows us to easily issue requests
31-
* to our Laravel back-end. This library automatically handles sending the
32-
* CSRF token as a header based on the value of the "XSRF" token cookie.
33-
*/
34-
3524
window.axios = axios;
3625

3726
window.axios.defaults.headers.common = {

resources/assets/js/components.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Modal from './components/Modal.vue';
2+
3+
Vue.component('modal', Modal);
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<template>
2+
<div>
3+
<!-- Button trigger modal -->
4+
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
5+
Create New Note
6+
</button>
7+
8+
<!-- Modal -->
9+
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
10+
<div class="modal-dialog" role="document">
11+
<div class="modal-content">
12+
<div class="modal-header">
13+
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
14+
<h4 class="modal-title" id="myModalLabel">Create New Note</h4>
15+
</div>
16+
<div class="modal-body">
17+
<p class="alert alert-success" v-if="success" v-text="success"></p>
18+
<form action="/note" method="post" @submit.prevent="createNote" @keydown="clearError($event.target.name)">
19+
<div :class="['form-group', errors.name ? 'has-error' : '']">
20+
<label for="name">Note Name</label>
21+
<input type="text" name="name" id="name" class="form-control" v-model="note.name">
22+
23+
<span class="help text-danger" v-if="errors.name">{{ errors.name[0] }}</span>
24+
</div>
25+
<div :class="['form-group', errors.description ? 'has-error' : '']">
26+
<label for="description">Note Description</label>
27+
<textarea name="description" id="description" class="form-control" v-model="note.description"></textarea>
28+
29+
<span class="help text-danger" v-if="errors.description">{{ errors.description[0] }}</span>
30+
</div>
31+
<button type="submit" class="btn btn-primary">Create</button>
32+
</form>
33+
</div>
34+
<div class="modal-footer">
35+
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
36+
</div>
37+
</div>
38+
</div>
39+
</div>
40+
</div>
41+
</template>
42+
43+
<script>
44+
export default {
45+
data: function () {
46+
return {
47+
note: {
48+
name: '',
49+
description: ''
50+
},
51+
errors: [],
52+
success: ''
53+
}
54+
},
55+
methods: {
56+
createNote: function () {
57+
let data = this.note;
58+
59+
axios.post('/note', data)
60+
.then(response => this.success = response.data.message)
61+
.catch(error => this.errors = error.response.data);
62+
63+
this.note = { name: '', description: '' };
64+
},
65+
clearError: function (field) {
66+
delete this.errors[field];
67+
}
68+
}
69+
}
70+
</script>

resources/assets/js/components/views/Notes.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<ul>
55
<li v-for="note in notes">{{ note.name }} - <strong>{{ note.user.name }}</strong></li>
66
</ul>
7+
<modal></modal>
78
</section>
89
</template>
910

@@ -15,7 +16,7 @@
1516
}
1617
},
1718
created: function () {
18-
axios.get('/api/note')
19+
axios.get('/note')
1920
.then(response => this.notes = response.data)
2021
.catch(error => console.log(error.response.data));
2122
}

routes/api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
Route::middleware('auth:api')->get('/user', function (Request $request) {
1717
return $request->user();
1818
});
19-
Route::resource('note', 'NoteController');
19+

routes/web.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
Route::get('/', function () {
1515
return view('welcome');
1616
});
17-
17+
Route::resource('note', 'NoteController');
1818

0 commit comments

Comments
 (0)