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

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

init commit Elena

  • Property mode set to 100644
File size: 5.8 KB
Line 
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 PetsController : BaseController
11 {
12 public PetsController(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 // kco
28 var query = from st in db.PetsObj
29 where st.usersid == customerClass.id
30 select st;
31
32 var userPets =
33 //db.PetsObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
34 await query.ToListAsync<PetsClass>();
35 return View(userPets);
36 } else
37 {
38 return View(db.PetsObj.ToList());
39 }
40
41 }
42
43 // GET: Customer/Details/5
44 public ActionResult Details(int? id)
45 {
46 if (id == null)
47 {
48 return RedirectToAction("NotExist", "Error");
49 }
50 PetsClass peClass = db.PetsObj.Find(id);
51 if (peClass == null)
52 {
53 return RedirectToAction("NotExist", "Error");
54 }
55 return View(peClass);
56 }
57
58 // GET: Customer/Create
59 //public ActionResult Create()
60 //{
61 // return View();
62 //}
63
64 public ActionResult Create()
65 {
66
67 return View();
68 }
69
70 // POST: Customer/Create
71 // To protect from overposting attacks, enable the specific properties you want to bind to, for
72 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
73 [HttpPost]
74 [ValidateAntiForgeryToken]
75 public async Task<ActionResult> CreateAsync([Bind(include: "id,color,description,dateofbirthday, usersid,typeofpetsid")] PetsClass peClass)
76 {
77 bool isAuthenticated = User.Identity.IsAuthenticated;
78 if (!isAuthenticated)
79 {
80 return RedirectToAction("AccessDenied", "Error");
81 }
82 if (ModelState.IsValid)
83 {
84 // peClass.dateofbirthday = DateTime.SpecifyKind(peClass.dateofbirthday, DateTimeKind.Utc);
85 var user = await _userManager.GetUserAsync(User);
86 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
87 peClass.usersid = customerClass.id;
88 // voa go pisav tuka na 18.02
89 // PetsClass.dateofbirthday = DateOnly.FromDateTime(DateTime.UtcNow);
90 db.PetsObj.Add(peClass);
91 db.SaveChanges();
92 return RedirectToAction("Index");
93 }
94
95 return View(peClass);
96 }
97
98 // GET: Customer/Edit/5
99 public ActionResult Edit(int? id)
100 {
101 if (id == null)
102 {
103 return RedirectToAction("NotExist", "Error");
104 }
105 PetsClass peClass = db.PetsObj.Find(id);
106 if (peClass == null)
107 {
108 return RedirectToAction("NotExist", "Error");
109 }
110 return View(peClass);
111 }
112
113 // POST: Customer/Edit/5
114 // To protect from overposting attacks, enable the specific properties you want to bind to, for
115 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
116 [HttpPost]
117 [ValidateAntiForgeryToken]
118 public async Task<ActionResult> EditAsync([Bind(include: "id,color,description,dateofbirthday, usersid,typeofpetsid")] PetsClass peClass)
119 {
120 bool isAuthenticated = User.Identity.IsAuthenticated;
121 if (!isAuthenticated)
122 {
123 return RedirectToAction("AccessDenied", "Error");
124 }
125
126 if (ModelState.IsValid)
127 {
128 //peClass.dateofbirthday = DateTime.SpecifyKind(peClass.dateofbirthday, DateTimeKind.Utc);
129
130 var user = await _userManager.GetUserAsync(User);
131 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
132 peClass.usersid = customerClass.id;
133 db.Entry(peClass).State = EntityState.Modified;
134 db.SaveChanges();
135 return RedirectToAction("Index");
136 }
137 return View(peClass);
138 }
139
140 // GET: Customer/Delete/5
141 public ActionResult Delete(int? id)
142 {
143 if (id == null)
144 {
145 return RedirectToAction("NotExist", "Error");
146 }
147 PetsClass peClass = db.PetsObj.Find(id);
148 if (peClass == null)
149 {
150 return RedirectToAction("NotExist", "Error");
151 }
152 return View(peClass);
153 }
154
155 // POST: Customer/Delete/5
156 [HttpPost, ActionName("Delete")]
157 [ValidateAntiForgeryToken]
158 public ActionResult DeleteConfirmed(int id)
159 {
160 PetsClass peClass = db.PetsObj.Find(id);
161 db.PetsObj.Remove(peClass);
162 db.SaveChanges();
163 return RedirectToAction("Index");
164 }
165
166 protected override void Dispose(bool disposing)
167 {
168 if (disposing)
169 {
170 db.Dispose();
171 }
172 base.Dispose(disposing);
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.