Skip to content

Commit 376c264

Browse files
committed
2 parents c3f4ef4 + 527212e commit 376c264

File tree

2 files changed

+119
-41
lines changed

2 files changed

+119
-41
lines changed

app/Http/Controllers/PostController.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use App\Models\Post;
7+
use Illuminate\Support\Facades\Validator;
78

89
class PostController extends Controller
910
{
@@ -40,14 +41,17 @@ public function index()
4041
*/
4142
public function store(Request $request)
4243
{
43-
$this->validate($request, [
44-
'user_id' => 'required|min:1',
44+
$rules = [
45+
'user_id' => 'required|integer|min:1',
4546
'title' => 'required|max:255',
4647
'description' => 'required'
47-
],
48-
[
49-
'user_id.min:1' => 'You have to select author correctly.'
48+
];
49+
$validator = Validator::make($request->all(), $rules, [
50+
'user_id.min' => 'You must use the author correctly.'
5051
]);
52+
if ($validator->fails()) {
53+
return response()->json(array("message"=> "The given data was invalid.", "errors" => $validator->getMessageBag()->getMessages()), 422);
54+
}
5155
$post = Post::create([
5256
'user_id' => $request->user_id,
5357
'title' => $request->title,
@@ -66,14 +70,17 @@ public function store(Request $request)
6670
*/
6771
public function update(Request $request, Post $post)
6872
{
69-
$this->validate($request, [
70-
'user_id' => 'required|min:1',
73+
$rules = [
74+
'user_id' => 'required|integer|min:1',
7175
'title' => 'required|max:255',
7276
'description' => 'required'
73-
],
74-
[
75-
'user_id.min:1' => 'You have to select author correctly.'
77+
];
78+
$validator = Validator::make($request->all(), $rules, [
79+
'user_id.min' => 'You must use the author correctly.'
7680
]);
81+
if ($validator->fails()) {
82+
return response()->json(array("message"=> "The given data was invalid.", "errors" => $validator->getMessageBag()->getMessages()), 422);
83+
}
7784
$post->update([
7885
'user_id' => $request->user_id,
7986
'title' => $request->title,

resources/js/pages/home.vue

+102-31
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
</b-form-group>
1313
<b-form-group
1414
label="Author:"
15-
label-for="author">
15+
label-for="user_name">
1616
<!-- <b-form-select id="author" name="author" :class="{ 'is-invalid': form.errors.has('author') }" v-model="form.author" :options="options"></b-form-select>-->
17-
<b-form-input id="author" name="author" :class="{ 'is-invalid': form.errors.has('author') }" v-model="form.user" list="my-list-id"></b-form-input>
17+
<b-form-input id="user_name" name="user_name" :class="{ 'is-invalid': form.errors.has('user_id') }" v-model="form.user_name" list="my-list-id"></b-form-input>
1818
<datalist id="my-list-id">
19-
<option v-for="option in options">{{ option.text }}</option>
19+
<option v-for="author in authors">{{ author.name }}</option>
2020
</datalist>
21-
<has-error :form="form" field="author" />
21+
<has-error :form="form" field="user_id" />
2222
</b-form-group>
2323
<b-form-group
2424
label="Description:"
@@ -40,6 +40,7 @@
4040
type="submit"
4141
variant="primary"
4242
class="float-right mr-2"
43+
@click="create_post()"
4344
>
4445
Save
4546
</b-button>
@@ -56,13 +57,13 @@
5657
</b-form-group>
5758
<b-form-group
5859
label="Author:"
59-
label-for="author">
60+
label-for="user_name">
6061
<!-- <b-form-select id="user" name="user" :class="{ 'is-invalid': editform.errors.has('user') }" v-model="editform.user" :options="options"></b-form-select>-->
61-
<b-form-input id="author" name="author" :class="{ 'is-invalid': form.errors.has('author') }" v-model="form.author" list="my-list-id"></b-form-input>
62+
<b-form-input id="user_name" name="user_name" :class="{ 'is-invalid': editform.errors.has('user_id') }" v-model="editform.user_name" list="my-list-id"></b-form-input>
6263
<datalist id="my-list-id">
63-
<option v-for="option in options">{{ option.text }}</option>
64+
<option v-for="author in authors">{{ author.name }}</option>
6465
</datalist>
65-
<has-error :form="editform" field="author" />
66+
<has-error :form="editform" field="user_id" />
6667
</b-form-group>
6768
<b-form-group
6869
label="Description:"
@@ -84,16 +85,17 @@
8485
type="submit"
8586
variant="primary"
8687
class="float-right mr-2"
88+
@click="save_post"
8789
>
8890
Save
8991
</b-button>
9092
</div>
9193
</template>
9294
</b-modal>
93-
<b-table striped hover :items="items">
94-
<template #cell(action)="data">
95+
<b-table striped hover :items="items" :fields="fields">
96+
<template #cell(id)="data">
9597
<b-button size="sm" @click="openModal(data.value)" variant="info">Edit</b-button>
96-
<b-button size="sm" @click="delete(data.value)" variant="danger">Delete</b-button>
98+
<b-button size="sm" @click="deleteItem(data.value)" variant="danger">Delete</b-button>
9799
</template>
98100
</b-table>
99101
</b-card>
@@ -108,40 +110,109 @@ export default {
108110
return {
109111
create_modal_show: false,
110112
edit_modal_show: false,
111-
items: [
112-
{ title: 'Title1', author: 'Dickerson', description: 'Macdonald', action: 2 },
113-
{ title: 'Title2', author: 'Larsen', description: 'Shaw', action: 1 },
114-
{ title: 'Title3', author: 'Geneva', description: 'Wilson', action: 3 },
115-
{ title: 'Title4', author: 'Jami', description: 'Carney', action: 4 }
116-
],
117-
options: [
118-
{ value: 'a', text: 'This is First option'},
119-
{ value: 'b', text: 'Selected Option'}
120-
],
113+
fields: ['title', 'user_name', 'description', 'action'],
114+
items: [],
115+
authors: [],
121116
form: new Form({
122117
title: '',
123-
author: "a",
118+
user_name: '',
119+
user_id: 0,
124120
description: ''
125121
}),
126122
editform: new Form({
127-
id: '',
123+
id: 0,
128124
title: '',
129-
author: "Geneva",
125+
user_name: '',
126+
user_id: 0,
130127
description: ''
131128
})
132129
}
133130
},
134131
methods: {
135-
openModal(id) {
132+
async loadAuth () {
133+
const { data } = await this.form.get('/api/users')
134+
this.authors = data;
135+
},
136+
async loadPosts () {
137+
const { data } = await this.form.get('/api/posts')
138+
this.items = data;
139+
},
140+
openModal(item_id) {
141+
let item = this.items.find(({id})=>id===item_id)
136142
this.edit_modal_show = true
137-
this.editform.id = "Title3"
138-
this.editform.title = "Title3"
139-
this.editform.author = "Geneva"
140-
this.editform.description = "Wilson"
143+
this.editform.id = item.id
144+
this.editform.title = item.title
145+
this.editform.user_name = item.user_name
146+
this.editform.description = item.description
141147
},
142-
delete(id) {
143-
148+
async create_post() {
149+
let user = this.authors.find(({name})=>name===this.form.user_name);
150+
let user_id = 0
151+
if (user) user_id = user.id
152+
this.form.user_id = user_id;
153+
const { data } = await this.form.post('/api/posts')
154+
if (data.id > 0)
155+
{
156+
this.$bvToast.toast('The post was created successfully', {
157+
title: `Notification`,
158+
variant: 'success',
159+
solid: true
160+
})
161+
this.create_modal_show = false;
162+
this.loadPosts();
163+
} else {
164+
this.$bvToast.toast('Post creating operation was failed', {
165+
title: `Notification`,
166+
variant: 'danger',
167+
solid: true
168+
})
169+
}
170+
},
171+
async save_post() {
172+
let user = this.authors.find(({name})=>name===this.editform.user_name);
173+
let user_id = 0
174+
if (user) user_id = user.id
175+
this.editform.user_id = user_id;
176+
const { data } = await this.editform.post('/api/posts/' + this.editform.id)
177+
if (data.id > 0)
178+
{
179+
this.$bvToast.toast('The post was updated successfully', {
180+
title: `Notification`,
181+
variant: 'success',
182+
solid: true
183+
})
184+
this.edit_modal_show = false;
185+
this.loadPosts();
186+
} else {
187+
this.$bvToast.toast('Post updating operation was failed', {
188+
title: `Notification`,
189+
variant: 'danger',
190+
solid: true
191+
})
192+
}
193+
},
194+
async deleteItem(id) {
195+
const { data } = await this.editform.delete('/api/posts/' + id)
196+
if (data.status === "success")
197+
{
198+
this.$bvToast.toast('The post was deleted successfully', {
199+
title: `Notification`,
200+
variant: 'success',
201+
solid: true
202+
})
203+
} else {
204+
this.$bvToast.toast('Post deleting operation was failed', {
205+
title: `Notification`,
206+
variant: 'danger',
207+
solid: true
208+
})
209+
}
210+
this.loadPosts();
144211
}
212+
},
213+
mounted() {
214+
this.loadAuth();
215+
this.loadPosts();
145216
}
146217
}
147218
</script>

0 commit comments

Comments
 (0)