source: PostgreSqlDotnetCore/Controllers/PetCaresController.cs@ 2639fab

main
Last change on this file since 2639fab was 2aea0fd, checked in by ElenaMoskova <elena.moskova99@…>, 4 months ago

init commit Elena

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Models;
5using System;
6using System.Net;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class PetCaresController : BaseController
11 {
12 public PetCaresController(UserManager<IdentityUser> userManager) : base(userManager)
13 {
14 }
15
16 // GET: Customer
17 public async Task<ActionResult> IndexAsync()
18 {
19 // check for permission
20 UsersClass customerClass = await getCrrentUser();
21 if (customerClass == null)
22 {
23 return RedirectToAction("AccessDenied", "Error");
24 }
25 if (customerClass.role_id == RoleConstants.Standard)
26 {
27 // query
28 var query = from st in db.PetCaresObj
29 where st.usersid == customerClass.id
30 select st;
31
32 var userPets =
33 //db.PetCaresObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
34 await query.ToListAsync<Pet_CaresClass>();
35
36 return View(userPets);
37
38 PetCareAllData petCareAllData = new PetCareAllData();
39 petCareAllData.PetCares = userPets;
40
41
42 // query
43 var queryVetCenters = from kk in db.VetCentersObj
44 select kk;
45
46 // query
47 var queryUsers = from st in db.CustomerObj
48 select st;
49
50 var users = await queryUsers.ToListAsync<UsersClass>();
51 petCareAllData.Users = users;
52
53 //var vetCenters = await queryVetCenters.ToListAsync<VetCenter>();
54 //petCareAllData.VetCenters = vetCenters;
55
56 return View(petCareAllData);
57 } else
58 {
59 return View(db.PetCaresObj.ToList());
60 }
61
62 }
63
64 // GET: Customer/Details/5
65 public ActionResult Details(int? id)
66 {
67 if (id == null)
68 {
69 return RedirectToAction("NotExist", "Error");
70 }
71 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
72 if (peClass == null)
73 {
74 return RedirectToAction("NotExist", "Error");
75 }
76 return View(peClass);
77 }
78
79 // GET: Customer/Create
80 //public ActionResult Create()
81 //{
82 // return View();
83 //}
84
85 public ActionResult Create()
86 {
87
88 return View();
89 }
90
91 // POST: Customer/Create
92 // To protect from overposting attacks, enable the specific properties you want to bind to, for
93 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
94 [HttpPost]
95 [ValidateAntiForgeryToken]
96 public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, usersid, vetcentersid")] Pet_CaresClass peClass)
97 {
98 bool isAuthenticated = User.Identity.IsAuthenticated;
99 if (!isAuthenticated)
100 {
101 return RedirectToAction("AccessDenied", "Error");
102 }
103 if (ModelState.IsValid)
104 {
105 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
106 var user = await _userManager.GetUserAsync(User);
107 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
108 peClass.usersid = customerClass.id;
109 db.PetCaresObj.Add(peClass);
110 db.SaveChanges();
111 return RedirectToAction("Index");
112 }
113
114 return View(peClass);
115 }
116
117 // GET: Customer/Edit/5
118 public ActionResult Edit(int? id)
119 {
120 if (id == null)
121 {
122 return RedirectToAction("NotExist", "Error");
123 }
124 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
125 if (peClass == null)
126 {
127 return RedirectToAction("NotExist", "Error");
128 }
129 return View(peClass);
130 }
131
132 // POST: Customer/Edit/5
133 // To protect from overposting attacks, enable the specific properties you want to bind to, for
134 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
135 [HttpPost]
136 [ValidateAntiForgeryToken]
137 public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid")] Pet_CaresClass peClass)
138 {
139 bool isAuthenticated = User.Identity.IsAuthenticated;
140 if (!isAuthenticated)
141 {
142 return RedirectToAction("AccessDenied", "Error");
143 }
144
145 if (ModelState.IsValid)
146 {
147 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
148 var user = await _userManager.GetUserAsync(User);
149 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
150 peClass.usersid = customerClass.id;
151 db.Entry(peClass).State = EntityState.Modified;
152 db.SaveChanges();
153 return RedirectToAction("Index");
154 }
155 return View(peClass);
156 }
157
158 // GET: Customer/Delete/5
159 public ActionResult Delete(int? id)
160 {
161 if (id == null)
162 {
163 return RedirectToAction("NotExist", "Error");
164 }
165 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
166 if (peClass == null)
167 {
168 return RedirectToAction("NotExist", "Error");
169 }
170 return View(peClass);
171 }
172
173 // POST: Customer/Delete/5
174 [HttpPost, ActionName("Delete")]
175 [ValidateAntiForgeryToken]
176 public ActionResult DeleteConfirmed(int id)
177 {
178 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
179 db.PetCaresObj.Remove(peClass);
180 db.SaveChanges();
181 return RedirectToAction("Index");
182 }
183
184 protected override void Dispose(bool disposing)
185 {
186 if (disposing)
187 {
188 db.Dispose();
189 }
190 base.Dispose(disposing);
191 }
192 }
193}
Note: See TracBrowser for help on using the repository browser.