-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.vue
More file actions
214 lines (192 loc) · 5.67 KB
/
app.vue
File metadata and controls
214 lines (192 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<template>
<div>
<h1>{{ name }}</h1>
<h2>{{ Title1 }}</h2>
<ul>
<li v-for="Project in Projects" :key="Project.name">
<h2>{{ Project.name }}</h2>
<p>{{ Project.description }}</p>
<p><b>ID:</b>{{ Project._id }}</p>
</li>
</ul>
<h2>{{ Title2 }}</h2>
<ul>
<li v-for="Blog in Blogs" :key="Blog.Title">
<h2>{{ Blog.Title }}</h2>
<p>{{ Blog.Content }}</p>
<p>{{ Blog.Date }}</p>
</li>
</ul>
</div>
<div>
<h1>Create a New Project</h1>
<form @submit.prevent="createProject">
<table>
<tr>
<td><label for="name">Project Name:</label></td>
<td><input type="text" v-model="newProject.name" id="name" required></td>
</tr>
<tr>
<td><label for="description">Project Description:</label></td>
<td><textarea id="description" v-model="newProject.description" required></textarea></td>
</tr>
<tr>
<td></td>
<td><button type="submit">Create Project</button></td>
</tr>
</table>
</form>
</div>
<div>
<h1>Update a Project</h1>
<form @submit.prevent="changeProject">
<table>
<tr>
<td><label for="id">Project ID(Identify by Name):</label></td>
<td><select v-model="updateProject.id" id="ProjecttID" required>
<option v-for="Project in Projects" :key="Project._id" :value="Project._id">{{ Project.name }}</option>
</select></td>
</tr>
<tr>
<td><label for="name">Project Name:</label></td>
<td><input type="text" v-model="updateProject.name" id="name" required></td>
</tr>
<tr>
<td><label for="description">Project Description:</label></td>
<td><textarea id="description" v-model="updateProject.description" required></textarea></td>
</tr>
<tr>
<td></td>
<td><button type="submit">Update Project</button></td>
</tr>
</table>
</form>
</div>
<div>
<h1>Delete a Project</h1>
<form @submit.prevent="removeProject">
<table>
<tr>
<td><label for="id">Project ID(Identify by Name):</label></td>
<td><select v-model="deleteProject.id" id="ProjecttID" required>
<option v-for="Project in Projects" :key="Project._id" :value="Project._id">{{ Project.name }}</option>
</select></td>
</tr>
<tr>
<td></td>
<td><button type="submit">Delete Project</button></td>
</tr>
</table>
</form>
</div>
</template>
<script setup>
const name = "Lasitha Hasaranga";
const Title1 = "Projects";
// const Projects = [
// {
// name : 'L1 Project',
// description : 'Object Identification Toy for Childeren',
// },
// {
// name : 'L2 Project',
// description : 'LMS System for Students in High School',
// },
// ];
const {data:Projects, pending1, error1} = useFetch('http://localhost:5000/Projects');
const Title2 = "Blogs";
const {data:Blogs, pending2, error2} = useFetch('http://localhost:5000/blogs');
//Create a new project
const newProject = ref({
name: '',
description: '',
});
//Function to create a new project
const createProject = async () => {
console.log(newProject.value);
try{
const response = await fetch('http://localhost:5000/Projects', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newProject.value),
});
if(!response.ok){
throw new Error('Failed to create a new project');
}
//Update the backend with new project
const result = await response.json();
Projects.value.push(result);//Add new project to the list
//clear the form
newProject.value.name = '';
newProject.value.description = '';
}catch(error){
console.error(error);
}
};
//Update a project
const updateProject = ref({
id: '',
name: '',
description: '',
});
//Function to Update the project using project id
const changeProject = async () => {
console.log(updateProject.value);
try{
const response = await fetch(`http://localhost:5000/Projects/${updateProject.value.id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updateProject.value),
});
if(!response.ok){
throw new Error('Failed to update the project');
}
//Update the backend with updated project
const result = await response.json();
//replace the element within project list by checking the _id
const index = Projects.value.findIndex((project) => project._id === updateProject.value.id);
if(index !== -1) {
Projects.value[index] = result;
}
//clear the form
updateProject.value.id = '';
updateProject.value.name = '';
updateProject.value.description = '';
}catch(error){
console.error(error);
}
};
//Delete a project
const deleteProject = ref({
id: '',
});
//Function to delete the project using project id
const removeProject = async () => {
console.log(deleteProject.value);
try{
const response = await fetch(`http://localhost:5000/Projects/${deleteProject.value.id}`, {
method: 'DELETE',
});
if(!response.ok){
throw new Error('Failed to delete the project');
}
//update the backend with deleted project
const result = await response.json();
//Remove the project from the list
const index = Projects.value.findIndex((project) => project._id === deleteProject.value.id);
if(index !== -1) {
Projects.value.splice(index, 1);
}
//clear the form
deleteProject.value.id = '';
}catch(error){
console.error(error);
}
};
</script>
<style>
</style>