1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using PostgreSqlDotnetCore.Data;
|
---|
5 | using PostgreSqlDotnetCore.Models;
|
---|
6 | using System.Net;
|
---|
7 |
|
---|
8 | namespace PostgreSqlDotnetCore.Controllers
|
---|
9 | {
|
---|
10 | public class BlogController : BaseController
|
---|
11 | {
|
---|
12 | public BlogController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
13 | {
|
---|
14 | }
|
---|
15 |
|
---|
16 | // GET: Customer
|
---|
17 | public async Task<ActionResult> IndexAsync()
|
---|
18 | {
|
---|
19 | // check for permission
|
---|
20 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
21 | if (!isAuthenticated)
|
---|
22 | {
|
---|
23 | return RedirectToAction("AccessDenied", "Error");
|
---|
24 | }
|
---|
25 | //return View(Enumerable.Empty<UsersClass>());
|
---|
26 | return View(db.BlogPostControllerObj.ToList());
|
---|
27 | }
|
---|
28 |
|
---|
29 | // GET: Customer/Details/5
|
---|
30 | public async Task<ActionResult> DetailsAsync(int? id)
|
---|
31 | {
|
---|
32 | if (id == null)
|
---|
33 | {
|
---|
34 | return View(null);
|
---|
35 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
36 | }
|
---|
37 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
38 | if (blogClass == null)
|
---|
39 | {
|
---|
40 | return RedirectToAction("NotExist", "Error");
|
---|
41 | }
|
---|
42 | // get answers
|
---|
43 |
|
---|
44 | // query
|
---|
45 | var query = from st in db.BlogPostAnswersObj
|
---|
46 | where st.blogpostconsultationid == blogClass.id
|
---|
47 | select st;
|
---|
48 | //elenaaa
|
---|
49 | var blogAnswers = query.ToList();
|
---|
50 | blogClass.BlogPostAnswers = blogAnswers;
|
---|
51 | return View(blogClass);
|
---|
52 | }
|
---|
53 |
|
---|
54 | // GET: Customer/Create
|
---|
55 | //public ActionResult Create()
|
---|
56 | //{
|
---|
57 | // return View();
|
---|
58 | //}
|
---|
59 |
|
---|
60 | public ActionResult Create()
|
---|
61 | {
|
---|
62 | var model = new BlogPostConsultation
|
---|
63 | {
|
---|
64 | //date_askes = DateTime.Now // Поставете го датумот на моменталниот датум
|
---|
65 | // date_askes= DateTime.UtcNow.Date
|
---|
66 | date_askes = DateOnly.FromDateTime(DateTime.UtcNow.Date)
|
---|
67 | };
|
---|
68 | return View(model);
|
---|
69 | }
|
---|
70 |
|
---|
71 | // POST: Customer/Create
|
---|
72 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
73 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
74 | [HttpPost]
|
---|
75 | [ValidateAntiForgeryToken]
|
---|
76 | public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
77 | {
|
---|
78 | if (ModelState.IsValid)
|
---|
79 | {
|
---|
80 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
81 | if (isAuthenticated)
|
---|
82 | {
|
---|
83 | var user = await _userManager.GetUserAsync(User);
|
---|
84 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
85 | // blogClass.date_askes = new DateTime();
|
---|
86 | // blogClass.date_askes = DateTime.UtcNow;
|
---|
87 | blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
|
---|
88 | blogClass.users_id = customerClass.id;
|
---|
89 | db.BlogPostControllerObj.Add(blogClass);
|
---|
90 | db.SaveChanges();
|
---|
91 | return RedirectToAction("Index");
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | return RedirectToAction("AccessDenied", "Error");
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | return View(blogClass);
|
---|
100 | }
|
---|
101 |
|
---|
102 | // GET: Customer/Edit/5
|
---|
103 | public async Task<ActionResult> EditAsync(int? id)
|
---|
104 | {
|
---|
105 | if (id == null)
|
---|
106 | {
|
---|
107 | return View(null);
|
---|
108 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
109 | }
|
---|
110 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
111 | if (blogClass == null)
|
---|
112 | {
|
---|
113 | return RedirectToAction("NotExist", "Error");
|
---|
114 | }
|
---|
115 |
|
---|
116 | // check for permission
|
---|
117 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
118 | if (customerClass == null)
|
---|
119 | {
|
---|
120 |
|
---|
121 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
122 | if (isAuthenticated)
|
---|
123 | {
|
---|
124 | var user = await _userManager.GetUserAsync(User);
|
---|
125 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
126 | if (blogClass.users_id != customerClass.id)
|
---|
127 | {
|
---|
128 | return RedirectToAction("AccessDenied", "Error");
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | return View(blogClass);
|
---|
134 | }
|
---|
135 |
|
---|
136 | // POST: Customer/Edit/5
|
---|
137 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
138 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
139 | [HttpPost]
|
---|
140 | [ValidateAntiForgeryToken]
|
---|
141 | public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
142 | {
|
---|
143 | if (ModelState.IsValid)
|
---|
144 | {
|
---|
145 | db.Entry(blogClass).State = EntityState.Modified;
|
---|
146 | db.SaveChanges();
|
---|
147 | return RedirectToAction("Index");
|
---|
148 | }
|
---|
149 | return View(blogClass);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // GET: Customer/Delete/5
|
---|
153 | public async Task<ActionResult> DeleteAsync(int? id)
|
---|
154 | {
|
---|
155 | if (id == null)
|
---|
156 | {
|
---|
157 | return View(null);
|
---|
158 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
159 | }
|
---|
160 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
161 | if (blogClass == null)
|
---|
162 | {
|
---|
163 | return View(null);
|
---|
164 | //return HttpNotFound();
|
---|
165 | }
|
---|
166 | // check for permission
|
---|
167 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
168 | if (customerClass == null)
|
---|
169 | {
|
---|
170 |
|
---|
171 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
172 | if (isAuthenticated)
|
---|
173 | {
|
---|
174 | var user = await _userManager.GetUserAsync(User);
|
---|
175 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
176 | if (blogClass.users_id != customerClass.id)
|
---|
177 | {
|
---|
178 | return RedirectToAction("AccessDenied", "Error");
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | return View(blogClass);
|
---|
183 | }
|
---|
184 |
|
---|
185 | // POST: Customer/Delete/5
|
---|
186 |
|
---|
187 |
|
---|
188 | [HttpPost, ActionName("Delete")]
|
---|
189 | [ValidateAntiForgeryToken]
|
---|
190 | public ActionResult DeleteConfirmed(int id)
|
---|
191 | {
|
---|
192 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
193 | db.BlogPostControllerObj.Remove(blogClass);
|
---|
194 | db.SaveChanges();
|
---|
195 | return RedirectToAction("Index");
|
---|
196 | }
|
---|
197 |
|
---|
198 | protected override void Dispose(bool disposing)
|
---|
199 | {
|
---|
200 | if (disposing)
|
---|
201 | {
|
---|
202 | db.Dispose();
|
---|
203 | }
|
---|
204 | base.Dispose(disposing);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|