1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using Microsoft.AspNetCore.Mvc.Rendering;
|
---|
5 | using PostgreSqlDotnetCore.Models;
|
---|
6 | using System;
|
---|
7 | using System.Net;
|
---|
8 |
|
---|
9 | namespace PostgreSqlDotnetCore.Controllers
|
---|
10 | {
|
---|
11 | public class PetCaresController : BaseController
|
---|
12 | {
|
---|
13 | public PetCaresController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | [HttpGet]
|
---|
19 | /* public async Task<ActionResult> Create()
|
---|
20 | {
|
---|
21 |
|
---|
22 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
23 |
|
---|
24 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
25 |
|
---|
26 | return View();
|
---|
27 | }*/
|
---|
28 | public async Task<ActionResult> Create()
|
---|
29 | {
|
---|
30 |
|
---|
31 |
|
---|
32 | UsersClass customerClass = await getCrrentUser();
|
---|
33 |
|
---|
34 | ViewBag.isAuthenticated = customerClass;
|
---|
35 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
36 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
37 |
|
---|
38 | return View();
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | // GET: Customer
|
---|
45 | public async Task<ActionResult> IndexAsync()
|
---|
46 | {
|
---|
47 | // check for permission
|
---|
48 | UsersClass customerClass = await getCrrentUser();
|
---|
49 | // set if is authenticated
|
---|
50 | ViewBag.isAuthenticated = customerClass;
|
---|
51 | if (customerClass == null)
|
---|
52 | {
|
---|
53 | return RedirectToAction("AccessDenied", "Error");
|
---|
54 | }
|
---|
55 | if (customerClass.role_id == RoleConstants.Standard)
|
---|
56 | {
|
---|
57 | // query
|
---|
58 | var query = from st in db.PetCaresObj
|
---|
59 | where st.usersid == customerClass.id
|
---|
60 | select st;
|
---|
61 |
|
---|
62 | var userPets =
|
---|
63 | //db.PetCaresObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
|
---|
64 | await query.ToListAsync<Pet_CaresClass>();
|
---|
65 |
|
---|
66 | return View(userPets);
|
---|
67 |
|
---|
68 | PetCareAllData petCareAllData = new PetCareAllData();
|
---|
69 | petCareAllData.PetCares = userPets;
|
---|
70 |
|
---|
71 |
|
---|
72 | // query
|
---|
73 | var queryVetCenters = from kk in db.VetCentersObj
|
---|
74 | select kk;
|
---|
75 |
|
---|
76 | // query
|
---|
77 | var queryUsers = from st in db.CustomerObj
|
---|
78 | select st;
|
---|
79 |
|
---|
80 | var users = await queryUsers.ToListAsync<UsersClass>();
|
---|
81 | petCareAllData.Users = users;
|
---|
82 |
|
---|
83 | //var vetCenters = await queryVetCenters.ToListAsync<VetCenter>();
|
---|
84 | //petCareAllData.VetCenters = vetCenters;
|
---|
85 |
|
---|
86 | return View(petCareAllData);
|
---|
87 | } else
|
---|
88 | {
|
---|
89 | return View(db.PetCaresObj.ToList());
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | // GET: Customer/Details/5
|
---|
95 | public async Task<ActionResult> Details(int? id)
|
---|
96 | {
|
---|
97 | if (id == null)
|
---|
98 | {
|
---|
99 | return RedirectToAction("NotExist", "Error");
|
---|
100 | }
|
---|
101 | UsersClass customerClass = await getCrrentUser();
|
---|
102 | ViewBag.isAuthenticated = customerClass;
|
---|
103 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
104 | if (peClass == null)
|
---|
105 | {
|
---|
106 | return RedirectToAction("NotExist", "Error");
|
---|
107 | }
|
---|
108 | return View(peClass);
|
---|
109 | }
|
---|
110 |
|
---|
111 | // GET: Customer/Create
|
---|
112 | //public ActionResult Create()
|
---|
113 | //{
|
---|
114 | // return View();
|
---|
115 | //}
|
---|
116 |
|
---|
117 | /*public ActionResult Create()
|
---|
118 | {
|
---|
119 |
|
---|
120 | return View();
|
---|
121 | }*/
|
---|
122 |
|
---|
123 | // POST: Customer/Create
|
---|
124 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
125 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
126 | [HttpPost]
|
---|
127 | [ValidateAntiForgeryToken]
|
---|
128 | public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
|
---|
129 | {
|
---|
130 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
131 | if (!isAuthenticated)
|
---|
132 | {
|
---|
133 | return RedirectToAction("AccessDenied", "Error");
|
---|
134 | }
|
---|
135 | ViewBag.isAuthenticated = new UsersClass();
|
---|
136 |
|
---|
137 | if (ModelState.IsValid)
|
---|
138 | {
|
---|
139 | ViewBag.isAuthenticated = new UsersClass();
|
---|
140 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
141 | peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
|
---|
142 | var user = await _userManager.GetUserAsync(User);
|
---|
143 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
144 | peClass.usersid = customerClass.id;
|
---|
145 | db.PetCaresObj.Add(peClass);
|
---|
146 | db.SaveChanges();
|
---|
147 | return RedirectToAction("Index");
|
---|
148 | }
|
---|
149 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
150 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
151 |
|
---|
152 | return View(peClass);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | // GET: Customer/Edit/5
|
---|
165 | /* public ActionResult Edit(int? id)
|
---|
166 | {
|
---|
167 | if (id == null)
|
---|
168 | {
|
---|
169 | return RedirectToAction("NotExist", "Error");
|
---|
170 | }
|
---|
171 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
172 | if (peClass == null)
|
---|
173 | {
|
---|
174 | return RedirectToAction("NotExist", "Error");
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | return View(peClass);
|
---|
179 | }*/
|
---|
180 | // GET: Customer/Edit/5
|
---|
181 | /* public ActionResult Edit(int? id)
|
---|
182 | {
|
---|
183 | if (id == null)
|
---|
184 | {
|
---|
185 | return RedirectToAction("NotExist", "Error");
|
---|
186 | }
|
---|
187 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
188 | if (peClass == null)
|
---|
189 | {
|
---|
190 | return RedirectToAction("NotExist", "Error");
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | return View(peClass);
|
---|
195 | }*/
|
---|
196 | public async Task<ActionResult> Edit(int? id)
|
---|
197 | {
|
---|
198 | if (id == null)
|
---|
199 | {
|
---|
200 | return RedirectToAction("NotExist", "Error");
|
---|
201 | }
|
---|
202 |
|
---|
203 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
204 | if (peClass == null)
|
---|
205 | {
|
---|
206 | return RedirectToAction("NotExist", "Error");
|
---|
207 | }
|
---|
208 |
|
---|
209 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
210 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
|
---|
211 | // dodadeno na 22.08
|
---|
212 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
213 | return View(peClass);
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 |
|
---|
218 | // POST: Customer/Edit/5
|
---|
219 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
220 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
221 | [HttpPost]
|
---|
222 | [ValidateAntiForgeryToken]
|
---|
223 | public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid")] Pet_CaresClass peClass)
|
---|
224 | {
|
---|
225 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
226 | if (!isAuthenticated)
|
---|
227 | {
|
---|
228 | return RedirectToAction("AccessDenied", "Error");
|
---|
229 | }
|
---|
230 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
231 |
|
---|
232 | if (ModelState.IsValid)
|
---|
233 | {
|
---|
234 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
235 | var user = await _userManager.GetUserAsync(User);
|
---|
236 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
237 | peClass.usersid = customerClass.id;
|
---|
238 | db.Entry(peClass).State = EntityState.Modified;
|
---|
239 | db.SaveChanges();
|
---|
240 | return RedirectToAction("Index");
|
---|
241 | }
|
---|
242 | return View(peClass);
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 | // GET: Customer/Delete/5
|
---|
249 | public async Task<ActionResult> Delete(int? id)
|
---|
250 | {
|
---|
251 | if (id == null)
|
---|
252 | {
|
---|
253 | return RedirectToAction("NotExist", "Error");
|
---|
254 | }
|
---|
255 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
256 | ViewBag.isAuthenticated = customerClass;
|
---|
257 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
258 | if (peClass == null)
|
---|
259 | {
|
---|
260 | return RedirectToAction("NotExist", "Error");
|
---|
261 | }
|
---|
262 | return View(peClass);
|
---|
263 | }
|
---|
264 |
|
---|
265 | // POST: Customer/Delete/5
|
---|
266 | [HttpPost, ActionName("Delete")]
|
---|
267 | [ValidateAntiForgeryToken]
|
---|
268 | public ActionResult DeleteConfirmed(int id)
|
---|
269 | {
|
---|
270 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
271 | db.PetCaresObj.Remove(peClass);
|
---|
272 | db.SaveChanges();
|
---|
273 | return RedirectToAction("Index");
|
---|
274 | }
|
---|
275 |
|
---|
276 | protected override void Dispose(bool disposing)
|
---|
277 | {
|
---|
278 | if (disposing)
|
---|
279 | {
|
---|
280 | db.Dispose();
|
---|
281 | }
|
---|
282 | base.Dispose(disposing);
|
---|
283 | }
|
---|
284 | }
|
---|
285 | }
|
---|