source: PostgreSqlDotnetCore/Controllers/PetCaresController.cs@ ae6c071

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

Аutomating the process

automating the process of updating the list of veterinary centers

  • Property mode set to 100644
File size: 6.9 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 return View(peClass);
144 }
145
146 // POST: Customer/Edit/5
147 // To protect from overposting attacks, enable the specific properties you want to bind to, for
148 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
149 [HttpPost]
150 [ValidateAntiForgeryToken]
151 public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, start_date vetcentersid")] Pet_CaresClass peClass)
152 {
153 bool isAuthenticated = User.Identity.IsAuthenticated;
154 if (!isAuthenticated)
155 {
156 return RedirectToAction("AccessDenied", "Error");
157 }
158
159 if (ModelState.IsValid)
160 {
161 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
162 peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
163 var user = await _userManager.GetUserAsync(User);
164 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
165 peClass.usersid = customerClass.id;
166 db.Entry(peClass).State = EntityState.Modified;
167 db.SaveChanges();
168 return RedirectToAction("Index");
169 }
170 return View(peClass);
171 }
172
173 // GET: Customer/Delete/5
174 public ActionResult Delete(int? id)
175 {
176 if (id == null)
177 {
178 return RedirectToAction("NotExist", "Error");
179 }
180 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
181 if (peClass == null)
182 {
183 return RedirectToAction("NotExist", "Error");
184 }
185 return View(peClass);
186 }
187
188 // POST: Customer/Delete/5
189 [HttpPost, ActionName("Delete")]
190 [ValidateAntiForgeryToken]
191 public ActionResult DeleteConfirmed(int id)
192 {
193 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
194 db.PetCaresObj.Remove(peClass);
195 db.SaveChanges();
196 return RedirectToAction("Index");
197 }
198
199 protected override void Dispose(bool disposing)
200 {
201 if (disposing)
202 {
203 db.Dispose();
204 }
205 base.Dispose(disposing);
206 }
207 }
208}
Note: See TracBrowser for help on using the repository browser.