source: PostgreSqlDotnetCore/Controllers/PetCaresController.cs@ 8f8226c

main
Last change on this file since 8f8226c was 8f8226c, checked in by ElenaMoskova <elena.moskova99@…>, 6 weeks ago

fix bugs and new pages

Fix BlogPostAnswers
fix present data in the lists

  • Property mode set to 100644
File size: 7.8 KB
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using Microsoft.AspNetCore.Mvc.Rendering;
5using PostgreSqlDotnetCore.Models;
6using System;
7using System.Net;
8
9namespace 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
29 // GET: Customer
30 public async Task<ActionResult> IndexAsync()
31 {
32 // check for permission
33 UsersClass customerClass = await getCrrentUser();
34 if (customerClass == null)
35 {
36 return RedirectToAction("AccessDenied", "Error");
37 }
38 if (customerClass.role_id == RoleConstants.Standard)
39 {
40 // query
41 var query = from st in db.PetCaresObj
42 where st.usersid == customerClass.id
43 select st;
44
45 var userPets =
46 //db.PetCaresObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
47 await query.ToListAsync<Pet_CaresClass>();
48
49 return View(userPets);
50
51 PetCareAllData petCareAllData = new PetCareAllData();
52 petCareAllData.PetCares = userPets;
53
54
55 // query
56 var queryVetCenters = from kk in db.VetCentersObj
57 select kk;
58
59 // query
60 var queryUsers = from st in db.CustomerObj
61 select st;
62
63 var users = await queryUsers.ToListAsync<UsersClass>();
64 petCareAllData.Users = users;
65
66 //var vetCenters = await queryVetCenters.ToListAsync<VetCenter>();
67 //petCareAllData.VetCenters = vetCenters;
68
69 return View(petCareAllData);
70 } else
71 {
72 return View(db.PetCaresObj.ToList());
73 }
74
75 }
76
77 // GET: Customer/Details/5
78 public ActionResult Details(int? id)
79 {
80 if (id == null)
81 {
82 return RedirectToAction("NotExist", "Error");
83 }
84 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
85 if (peClass == null)
86 {
87 return RedirectToAction("NotExist", "Error");
88 }
89 return View(peClass);
90 }
91
92 // GET: Customer/Create
93 //public ActionResult Create()
94 //{
95 // return View();
96 //}
97
98 /*public ActionResult Create()
99 {
100
101 return View();
102 }*/
103
104 // POST: Customer/Create
105 // To protect from overposting attacks, enable the specific properties you want to bind to, for
106 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
107 [HttpPost]
108 [ValidateAntiForgeryToken]
109 public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
110 {
111 bool isAuthenticated = User.Identity.IsAuthenticated;
112 if (!isAuthenticated)
113 {
114 return RedirectToAction("AccessDenied", "Error");
115 }
116 if (ModelState.IsValid)
117 {
118 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
119 peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
120 var user = await _userManager.GetUserAsync(User);
121 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
122 peClass.usersid = customerClass.id;
123 db.PetCaresObj.Add(peClass);
124 db.SaveChanges();
125 return RedirectToAction("Index");
126 }
127
128 return View(peClass);
129 }
130
131 // GET: Customer/Edit/5
132 /* public ActionResult Edit(int? id)
133 {
134 if (id == null)
135 {
136 return RedirectToAction("NotExist", "Error");
137 }
138 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
139 if (peClass == null)
140 {
141 return RedirectToAction("NotExist", "Error");
142 }
143
144
145 return View(peClass);
146 }*/
147 // GET: Customer/Edit/5
148 /* public ActionResult Edit(int? id)
149 {
150 if (id == null)
151 {
152 return RedirectToAction("NotExist", "Error");
153 }
154 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
155 if (peClass == null)
156 {
157 return RedirectToAction("NotExist", "Error");
158 }
159
160
161 return View(peClass);
162 }*/
163 public async Task<ActionResult> Edit(int? id)
164 {
165 if (id == null)
166 {
167 return RedirectToAction("NotExist", "Error");
168 }
169
170 Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
171 if (peClass == null)
172 {
173 return RedirectToAction("NotExist", "Error");
174 }
175
176 var vetCenters = await db.VetCentersObj.ToListAsync();
177 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
178
179 return View(peClass);
180 }
181
182
183
184 // POST: Customer/Edit/5
185 // To protect from overposting attacks, enable the specific properties you want to bind to, for
186 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
187 [HttpPost]
188 [ValidateAntiForgeryToken]
189 public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid")] Pet_CaresClass peClass)
190 {
191 bool isAuthenticated = User.Identity.IsAuthenticated;
192 if (!isAuthenticated)
193 {
194 return RedirectToAction("AccessDenied", "Error");
195 }
196
197 if (ModelState.IsValid)
198 {
199 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
200 var user = await _userManager.GetUserAsync(User);
201 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
202 peClass.usersid = customerClass.id;
203 db.Entry(peClass).State = EntityState.Modified;
204 db.SaveChanges();
205 return RedirectToAction("Index");
206 }
207 return View(peClass);
208 }
209
210
211
212
213 // GET: Customer/Delete/5
214 public ActionResult Delete(int? id)
215 {
216 if (id == null)
217 {
218 return RedirectToAction("NotExist", "Error");
219 }
220 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
221 if (peClass == null)
222 {
223 return RedirectToAction("NotExist", "Error");
224 }
225 return View(peClass);
226 }
227
228 // POST: Customer/Delete/5
229 [HttpPost, ActionName("Delete")]
230 [ValidateAntiForgeryToken]
231 public ActionResult DeleteConfirmed(int id)
232 {
233 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
234 db.PetCaresObj.Remove(peClass);
235 db.SaveChanges();
236 return RedirectToAction("Index");
237 }
238
239 protected override void Dispose(bool disposing)
240 {
241 if (disposing)
242 {
243 db.Dispose();
244 }
245 base.Dispose(disposing);
246 }
247 }
248}
Note: See TracBrowser for help on using the repository browser.