Ignore:
Timestamp:
08/28/24 21:41:13 (4 weeks ago)
Author:
ElenaMoskova <elena.moskova99@…>
Branches:
main
Children:
a850333
Parents:
118e414
Message:

Use of views

  1. Use of views in VetCenters.
  2. Ability to provide a response to a given response.
Location:
PostgreSqlDotnetCore/Controllers
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • PostgreSqlDotnetCore/Controllers/BlogController.cs

    r118e414 re9bb9d1  
    8989            }
    9090            BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
     91            UsersClass customerClass = await getCrrentUser();
     92            ViewBag.isAuthenticated = customerClass;
    9193            if (blogClass == null)
    9294            {
     
    102104            var blogAnswers = query.ToList();
    103105            blogClass.BlogPostAnswers = blogAnswers;
     106            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    104107            return View(blogClass);
    105108        }
     
    121124
    122125            // check for permission
    123             UsersClass customerClass = await getCrrentUser();
     126            //UsersClass customerClass = await checkAuthorizationAsync();
     127           // ViewBag.isAuthenticated = await getCrrentUser();
     128           UsersClass customerClass = await getCrrentUser();
    124129            // set if is authenticated
    125130            ViewBag.isAuthenticated = customerClass;
     
    143148                    var user = await _userManager.GetUserAsync(User);
    144149                    var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
     150                   //dodadeno na 25.08
     151                    ViewBag.isAuthenticated = await getCrrentUser();
    145152                    if (customerClass != null)
    146153                    {
     
    184191            }
    185192
    186             // check for permission
    187             UsersClass customerClass = await checkAuthorizationAsync();
    188             //dodadeno na 23.08
     193            // izbriseno na 26.08
     194           UsersClass customerClass = await checkAuthorizationAsync();
     195            //dodadeno na 26.08
     196           // UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
    189197            ViewBag.isAuthenticated = await getCrrentUser();
    190198            if (customerClass == null)
     
    204212            // no access for standard user
    205213            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     214            ViewBag.OnlyAdminManager1 = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    206215
    207216
     
    233242                if (existingBlogClass != null)
    234243                {
     244                   
     245
    235246                    // Запамтете ја старата вредност на users_id
    236247                    blogClass.users_id = existingBlogClass.users_id;
     
    247258
    248259        // GET: Customer/Delete/5
     260       
    249261        public async Task<ActionResult> DeleteAsync(int? id)
    250262        {
     
    286298        }
    287299
     300
    288301        // POST: Customer/Delete/5
    289302
     
    298311            return RedirectToAction("Index");
    299312        }
     313       
     314        // GET: Customer/Delete/5
     315        // GET: Customer/Delete/5
     316        // GET: Customer/Delete/5
     317   
     318
    300319
    301320        protected override void Dispose(bool disposing)
  • PostgreSqlDotnetCore/Controllers/BlogPostAnswersController.cs

    r118e414 re9bb9d1  
    1414
    1515        // GET: Customer
    16         public ActionResult Index()
    17         {
    18             //return View(Enumerable.Empty<UsersClass>());
     16        //public ActionResult Index()
     17         public async Task<ActionResult> IndexAsync()
     18         {
     19             //return View(Enumerable.Empty<UsersClass>());
     20             UsersClass customerClass = await getCrrentUser();
     21
     22             // set if is authenticated
     23             ViewBag.isAuthenticated = customerClass;
     24            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    1925            return View(db.BlogPostAnswersObj.ToList());
    2026        }
    2127
     28
     29
     30
    2231        // GET: Customer/Details/5
    23         public ActionResult Details(int? id)
     32        // public ActionResult Details(int? id)
     33        public async Task<ActionResult> Details(int? id)
    2434        {
    2535            if (id == null)
     
    2737                return RedirectToAction("NotExist", "Error");
    2838            }
     39            UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
     40            ViewBag.isAuthenticated = customerClass;
    2941            BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
    3042            if (answerClass == null)
     
    3244                return RedirectToAction("NotExist", "Error");
    3345            }
     46
     47
     48            // query
     49            var query = from st in db.BlogPostAnswersObj
     50                        where st.parent_id == answerClass.id
     51                        select st;
     52            //elenaaa
     53            var answersUnderA = query.Where(x => x.id != answerClass.id).ToList();
     54
     55            answerClass.blogPostAnswers = answersUnderA;
     56            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    3457            return View(answerClass);
    3558        }
     
    4669            // check for permission
    4770            UsersClass customerClass = await checkAuthorizationAsync();
     71           // UsersClass customerClass = await getCrrentUser();
     72            // set if is authenticated
     73            ViewBag.isAuthenticated = customerClass;
    4874            if (customerClass == null)
    4975            {
    5076                return RedirectToAction("AccessDenied", "Error");
    5177            }
     78            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    5279            return View();
    5380        }
     
    90117                return RedirectToAction("Details", "Blog", new { id });
    91118            }
     119            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    92120
    93121            return View(answerClass);
     
    139167        public async Task<ActionResult> DeleteAsync(int? id)
    140168        {
     169         
    141170            if (id == null)
    142171            {
     
    159188                }
    160189            }
    161             return View(answerClass);
     190            // return View(answerClass);
     191             return View(answerClass);
    162192        }
    163193
  • PostgreSqlDotnetCore/Controllers/HomeController.cs

    r118e414 re9bb9d1  
    11using Microsoft.AspNetCore.Identity;
    22using Microsoft.AspNetCore.Mvc;
     3using Microsoft.EntityFrameworkCore;
    34using PostgreSqlDotnetCore.Data;
    45using PostgreSqlDotnetCore.Models;
     
    3031                if (user != null)
    3132                {
    32                     UsersClass customerClass = db.CustomerObj.SingleOrDefault(x=> x.email == user.Email);
     33                    UsersClass customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
    3334                    if (customerClass == null)
    3435                    {
     
    4344                                lastName = nameLastName[0].Split('.')[1];
    4445                            }
    45                         } catch(Exception ex) {
     46                        }
     47                        catch (Exception ex)
     48                        {
    4649                        }
    4750                        db.CustomerObj.Add(new UsersClass(
     
    6568                }
    6669
    67             } else
     70            }
     71            else
    6872            {
    6973                ViewBag.isAuthenticated = null;
     
    8690        public async Task<IActionResult> ContactAsync()
    8791        {
     92
     93            //var query = db.Database.ExecuteSqlRaw("CALL get_pet_details()");
     94            //var query = db.Database.ExecuteSqlRaw("SELECT * FROM get_pet_details()");
     95
     96
    8897            // set if is authenticated
    8998            ViewBag.isAuthenticated = await getCrrentUser();
  • PostgreSqlDotnetCore/Controllers/PetCaresController.cs

    r118e414 re9bb9d1  
    3636            ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
    3737
     38
     39            // check if the user is authenticated so we can take only his pets
     40            if (customerClass != null)
     41            {
     42                var queryPetsByUser = from st in db.PetsObj
     43                                      where st.usersid == customerClass.id
     44                                      select st;
     45                var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
     46                ViewBag.Pets = new SelectList(userPets, "id", "name");
     47
     48            }
    3849            return View();
    3950        }
     
    6374                            select st;
    6475
    65                 var userPets =
    66                     //db.PetCaresObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
     76                var userPetCares =
    6777                    await query.ToListAsync<Pet_CaresClass>();
    6878
    69                 return View(userPets);
    70 
    71                 PetCareAllData petCareAllData = new PetCareAllData();
    72                 petCareAllData.PetCares = userPets;
    73 
    74 
    75                 // query
    76                 var queryVetCenters = from kk in db.VetCentersObj
    77                                       select kk;
    78 
    79                 // query
    80                 var queryUsers = from st in db.CustomerObj
    81                                  select st;
    82 
    83                 var users = await queryUsers.ToListAsync<UsersClass>();
    84                 petCareAllData.Users = users;
    85 
    86                 //var vetCenters = await queryVetCenters.ToListAsync<VetCenter>();
    87                 //petCareAllData.VetCenters = vetCenters;
    88 
    89                 return View(petCareAllData);
     79                return View(userPetCares);
    9080            }
    9181            else
     
    133123        [HttpPost]
    134124        [ValidateAntiForgeryToken]
    135         public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
     125        public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid, pet_id")] Pet_CaresClass peClass)
    136126        {
    137127            bool isAuthenticated = User.Identity.IsAuthenticated;
     
    143133            // no access for standard user
    144134            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    145 
     135            UsersClass customerClass =  null;
    146136            if (ModelState.IsValid)
    147137            {
     
    150140                peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
    151141                var user = await _userManager.GetUserAsync(User);
    152                 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
     142                customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
    153143                peClass.usersid = customerClass.id;
    154144                db.PetCaresObj.Add(peClass);
     
    159149            ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
    160150
    161             return View(peClass);
    162         }
    163 
    164 
    165 
    166 
    167 
    168 
    169 
     151
     152            var queryPetsByUser = from st in db.PetsObj
     153                                  where st.usersid == customerClass.id
     154                                  select st;
     155            var userPets =await queryPetsByUser.ToListAsync<PetsClass>();
     156            ViewBag.Pets= new SelectList(userPets, "id", "name");
     157
     158
     159            return View(peClass);
     160        }
    170161
    171162
     
    219210            ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
    220211            // dodadeno na 22.08
    221             ViewBag.isAuthenticated = await getCrrentUser();
     212            UsersClass customerClass = await getCrrentUser();
     213            // check if the user is authenticated so we can take only his pets
     214            if (customerClass != null)
     215            {
     216                var queryPetsByUser = from st in db.PetsObj
     217                                      where st.usersid == customerClass.id
     218                                      select st;
     219                var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
     220                ViewBag.Pets = new SelectList(userPets, "id", "name");
     221
     222            }
     223            ViewBag.isAuthenticated = customerClass;
    222224            return View(peClass);
    223225        }
     
    230232        [HttpPost]
    231233        [ValidateAntiForgeryToken]
    232         public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid")] Pet_CaresClass peClass)
     234        public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
    233235        {
    234236            bool isAuthenticated = User.Identity.IsAuthenticated;
  • PostgreSqlDotnetCore/Controllers/PetsController.cs

    r118e414 re9bb9d1  
    3131            if (customerClass.role_id == RoleConstants.Standard)
    3232            {
    33                 // kco
     33                // filter user pets by UserID
    3434                var query = from st in db.PetsObj
    3535                            where st.usersid == customerClass.id
  • PostgreSqlDotnetCore/Controllers/ProductsController.cs

    r118e414 re9bb9d1  
    4848        }
    4949
     50       
     51
    5052        // GET: Customer/Details/5
    5153        public async Task<ActionResult> Details(int? id)
     
    5557                return RedirectToAction("NotExist", "Error");
    5658            }
    57             UsersClass customerClass = await getCrrentUser();
     59            // додано на 28.08
     60         
     61
     62
     63
     64          UsersClass customerClass = await getCrrentUser();
    5865            ViewBag.isAuthenticated = customerClass;
    5966            ProductsClass prodClass = db.ProductObj.Find(id);
  • PostgreSqlDotnetCore/Controllers/VetCenterController.cs

    r118e414 re9bb9d1  
    55using Microsoft.AspNetCore.Mvc.Rendering;
    66using System.Threading.Tasks;
     7using Npgsql;
     8using PostgreSqlDotnetCore.Data;
    79
    810namespace PostgreSqlDotnetCore.Controllers
     
    1012    public class VetCenterController : BaseController
    1113    {
    12         public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
    13         {
     14        /* 
     15         public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
     16           {
     17           }
     18        */
     19        private readonly ApplicationDbContext db;
     20
     21        public VetCenterController(UserManager<IdentityUser> userManager, ApplicationDbContext context) : base(userManager)
     22        {
     23            db = context ?? throw new ArgumentNullException(nameof(context));
    1424        }
    1525
     
    4555             return View(vetCenters);
    4656         }*/
     57        /* public async Task<ActionResult> Index()
     58         {
     59             var vetCenters = await db.VetCentersObj.ToListAsync();
     60             ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
     61             // no access for standard user
     62             ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     63
     64             // Проверете дали корисникот е администратор или менаџер
     65             UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
     66             // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     67
     68             ViewBag.hasAccess = customerClass != null;
     69
     70             return View(vetCenters);
     71         }
     72     */
    4773        public async Task<ActionResult> Index()
    4874        {
    49             var vetCenters = await db.VetCentersObj.ToListAsync();
     75            var vetCenters = await db.VetCentersWithCity.ToListAsync();
    5076            ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
    5177            // no access for standard user
    5278            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    5379
    54             // Проверете дали корисникот е администратор или менаџер
     80           
    5581            UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
    56                // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    5782
    5883            ViewBag.hasAccess = customerClass != null;
     
    6287
    6388
    64         public async Task<ActionResult> Details(int? id)
     89
     90        /*public async Task<ActionResult> Details(int? id)
    6591        {
    6692            if (id == null)
     
    82108        }
    83109
     110
     111       */
     112        public async Task<IActionResult> Details(int? id)
     113        {
     114            if (id == null)
     115            {
     116                return RedirectToAction("NotExist", "Error");
     117            }
     118
     119            // Логирајте го ID-то за дебугирање
     120            Console.WriteLine($"ID: {id}");
     121
     122            // Обидете се да најдете запис во view
     123            VetCenterWithCity vetClass = await db.VetCentersWithCity
     124                                                 .Where(v => v.id == id)
     125                                                 .FirstOrDefaultAsync();
     126            if (vetClass == null)
     127            {
     128                return RedirectToAction("NotExist", "Error");
     129            }
     130
     131           
     132            UsersClass customerClass = await getCrrentUser();
     133            ViewBag.isAuthenticated = customerClass;
     134
     135           
     136            return View(vetClass);
     137        }
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
    84150        [HttpPost]
    85151        [ValidateAntiForgeryToken]
     
    99165            return View(vetClass);
    100166        }
     167
     168   
     169
     170         /*public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
     171         {
     172             // Логирање на параметрите
     173             Console.WriteLine($"Parameters: {vetClass.name}, {vetClass.adress}, {vetClass.description}, {vetClass.workinghours}, {vetClass.phonenumber}, {vetClass.latitude}, {vetClass.longitude}, {vetClass.citiesid}");
     174
     175             if (ModelState.IsValid)
     176             {
     177                 // Повик на складираната процедура
     178                 var parameters = new[]
     179                 {
     180             new NpgsqlParameter("@name", vetClass.name),
     181             new NpgsqlParameter("@adress", vetClass.adress),
     182             new NpgsqlParameter("@description", vetClass.description),
     183             new NpgsqlParameter("@workinghours", vetClass.workinghours),
     184             new NpgsqlParameter("@phonenumber", vetClass.phonenumber),
     185             new NpgsqlParameter("@latitude", (decimal)vetClass.latitude),
     186             new NpgsqlParameter("@longitude", (decimal)vetClass.longitude),
     187             new NpgsqlParameter("@citiesid", vetClass.citiesid)
     188         };
     189
     190                 await db.Database.ExecuteSqlRawAsync("CALL project.AddVetCenter(@name, @adress, @description, @workinghours, @phonenumber, @latitude, @longitude, @citiesid)", parameters);
     191
     192                 return RedirectToAction("Index");
     193             }
     194
     195             // Ако моделот не е валиден, повторно пополнете ги градовите за паѓачкиот мени
     196             var citiess = await db.CitiesObj.ToListAsync();
     197             ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
     198
     199             return View(vetClass);
     200         }
     201
     202         
     203
     204
     205        */
     206
     207
     208
    101209
    102210        public async Task<ActionResult> Edit(int? id)
     
    149257        }
    150258
    151         public async Task<ActionResult> Delete(int? id) {
    152              UsersClass customerClass = await checkAuthorizationAsync();
    153        
    154         ViewBag.isAuthenticated = await getCrrentUser();
    155        
     259        public async Task<ActionResult> Delete(int? id)
     260        {
     261            UsersClass customerClass = await checkAuthorizationAsync();
     262
     263            ViewBag.isAuthenticated = await getCrrentUser();
     264
    156265            if (id == null)
    157266            {
Note: See TracChangeset for help on using the changeset viewer.