Ignore:
Timestamp:
08/23/24 03:03:32 (5 weeks ago)
Author:
ElenaMoskova <elena.moskova99@…>
Branches:
main
Children:
118e414
Parents:
57fc402
Message:

Providing access

Providing access to various functionalities

File:
1 edited

Legend:

Unmodified
Added
Removed
  • PostgreSqlDotnetCore/Controllers/BlogController.cs

    r57fc402 r72b1da2  
    3030        */
    3131
    32         public async Task<ActionResult> Index()
    33         {
    34             // Проверка за автентикација
    35             bool isAuthenticated = User.Identity.IsAuthenticated;
    36 
    37             if (!isAuthenticated)
    38             {
    39                 return RedirectToAction("AccessDenied", "Error");
    40             }
    41 
    42             // Список на блог постови
    43             var blogPosts = await db.BlogPostControllerObj.ToListAsync();
    44 
    45             // Предавање на ViewBag за проверка на автентикација
    46             ViewBag.isAuthenticated = isAuthenticated;
    47 
    48             return View(blogPosts);
    49         }
     32        /* public async Task<ActionResult> Index()
     33         {
     34             // Проверка за автентикација
     35             bool isAuthenticated = User.Identity.IsAuthenticated;
     36
     37             if (!isAuthenticated)
     38             {
     39                 return RedirectToAction("AccessDenied", "Error");
     40             }
     41
     42             // Список на блог постови
     43             var blogPosts = await db.BlogPostControllerObj.ToListAsync();
     44
     45             // Предавање на ViewBag за проверка на автентикација
     46             ViewBag.isAuthenticated = isAuthenticated;
     47
     48             return View(blogPosts);
     49         }*/
     50
     51          public async Task<ActionResult> Index()
     52          {
     53              // Проверка за автентикација
     54              bool isAuthenticated = User.Identity.IsAuthenticated;
     55
     56              if (!isAuthenticated)
     57              {
     58                  return RedirectToAction("AccessDenied", "Error");
     59              }
     60
     61              // Список на блог постови
     62              var blogPosts = await db.BlogPostControllerObj.ToListAsync();
     63
     64              // Вземи тековниот корисник
     65              var currentUser = await _userManager.GetUserAsync(User);
     66              var customerClass = await db.CustomerObj.SingleOrDefaultAsync(x => x.email == currentUser.Email);
     67
     68              // Предавање на ViewBag за проверка на автентикација и корисничкиот ID
     69              ViewBag.isAuthenticated = isAuthenticated;
     70              ViewBag.CurrentUserId = customerClass?.id;
     71
     72              return View(blogPosts);
     73          }
     74       
     75
     76       
     77
     78
    5079        // GET: Customer/Details/5
    5180        public async Task<ActionResult> DetailsAsync(int? id)
     
    100129        [HttpPost]
    101130        [ValidateAntiForgeryToken]
    102          public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
    103          {
    104              if (ModelState.IsValid)
    105              {
    106                  bool isAuthenticated = User.Identity.IsAuthenticated;
    107                  if (isAuthenticated)
    108                  {
    109                      var user = await _userManager.GetUserAsync(User);
    110                      var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
    111                      if (customerClass != null)
    112                      {
    113                          // Поставете users_id на идентификаторот на корисникот
    114                          blogClass.users_id = customerClass.id;
     131        public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     132        {
     133            if (ModelState.IsValid)
     134            {
     135                bool isAuthenticated = User.Identity.IsAuthenticated;
     136                if (isAuthenticated)
     137                {
     138                    var user = await _userManager.GetUserAsync(User);
     139                    var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
     140                    if (customerClass != null)
     141                    {
     142                        // Поставете users_id на идентификаторот на корисникот
     143                        blogClass.users_id = customerClass.id;
    115144                        //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
    116145                        blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време
    117146
    118147                        db.BlogPostControllerObj.Add(blogClass);
    119                          await db.SaveChangesAsync();
    120                          return RedirectToAction("Index");
    121                      }
    122                  }
    123                  else
    124                  {
    125                      return RedirectToAction("AccessDenied", "Error");
    126                  }
    127              }
    128 
    129              return View(blogClass);
    130          }
    131 
    132        
    133        
     148                        await db.SaveChangesAsync();
     149                        return RedirectToAction("Index");
     150                    }
     151                }
     152                else
     153                {
     154                    return RedirectToAction("AccessDenied", "Error");
     155                }
     156            }
     157
     158            return View(blogClass);
     159        }
     160
     161
     162
     163
    134164
    135165
     
    151181            // check for permission
    152182            UsersClass customerClass = await checkAuthorizationAsync();
     183            //dodadeno na 23.08
     184            ViewBag.isAuthenticated = await getCrrentUser();
    153185            if (customerClass == null)
    154186            {
     
    174206        [HttpPost]
    175207        [ValidateAntiForgeryToken]
    176         public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     208        /* public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     209         {
     210             if (ModelState.IsValid)
     211             {
     212                 db.Entry(blogClass).State = EntityState.Modified;
     213                 db.SaveChanges();
     214                 return RedirectToAction("Index");
     215             }
     216             return View(blogClass);
     217         }*/
     218
     219       
     220        public async Task<ActionResult> EditAsync(int id, [Bind(include: "id,date_askes,title,description")] BlogPostConsultation blogClass)
    177221        {
    178222            if (ModelState.IsValid)
    179223            {
    180                 db.Entry(blogClass).State = EntityState.Modified;
    181                 db.SaveChanges();
    182                 return RedirectToAction("Index");
    183             }
    184             return View(blogClass);
    185         }
     224                var existingBlogClass = await db.BlogPostControllerObj.FindAsync(id);
     225                if (existingBlogClass != null)
     226                {
     227                    // Запамтете ја старата вредност на users_id
     228                    blogClass.users_id = existingBlogClass.users_id;
     229
     230                    db.Entry(existingBlogClass).CurrentValues.SetValues(blogClass);
     231                    await db.SaveChangesAsync();
     232                    return RedirectToAction("Index");
     233                }
     234            }
     235            return View(blogClass);
     236        }
     237
     238
    186239
    187240        // GET: Customer/Delete/5
    188         public async Task<ActionResult> DeleteAsync(int? id)
    189         {
     241        public async Task<ActionResult> DeleteAsync(int? id) {
     242          //  UsersClass customerClass = await checkAuthorizationAsync();
     243
     244        ViewBag.isAuthenticated = await getCrrentUser();
     245       
    190246            if (id == null)
    191247           {
Note: See TracChangeset for help on using the changeset viewer.