Changeset e90ba32


Ignore:
Timestamp:
08/29/24 20:27:21 (4 weeks ago)
Author:
ElenaMoskova <elena.moskova99@…>
Branches:
main
Children:
99d0ecc
Parents:
a850333
Message:

fix issues

fix bugs with nested tables
fix delete nested fk items

Location:
PostgreSqlDotnetCore
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • PostgreSqlDotnetCore/Controllers/BlogController.cs

    ra850333 re90ba32  
    6060
    6161            // Список на блог постови
    62             var blogPosts = await db.BlogPostControllerObj.ToListAsync();
     62            var blogPosts = await db.BlogUsers.ToListAsync();
    6363
    6464            // Вземи тековниот корисник
     
    105105            blogClass.BlogPostAnswers = blogAnswers;
    106106            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     107            ViewBag.CurrentUserId = customerClass?.id;
    107108            return View(blogClass);
    108109        }
     
    301302        // POST: Customer/Delete/5
    302303
     304        /*
     305                [HttpPost, ActionName("Delete")]
     306                [ValidateAntiForgeryToken]
     307                public ActionResult DeleteConfirmed(int id)
     308                {
     309                    BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
     310                    db.BlogPostControllerObj.Remove(blogClass);
     311                    db.SaveChanges();
     312                    return RedirectToAction("Index");
     313                }
     314                */
    303315
    304316        [HttpPost, ActionName("Delete")]
     
    306318        public ActionResult DeleteConfirmed(int id)
    307319        {
     320            // Наоѓање на објектот по ID
    308321            BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
     322
     323            // Проверка дали објектот е пронајден
     324            if (blogClass == null)
     325            {
     326                // Ако објектот не е пронајден, враќаме 404 Not Found или друга соодветна акција
     327                return View(null);
     328            }
     329            //  prvo izbrisi gi site odgovori po sot BlogId e primaren kluc vo drugata tabela
     330
     331            // query
     332            var query = from st in db.BlogPostAnswersObj
     333                        where st.BlogPostConsultationid == blogClass.id
     334                        select st;
     335            //elenaaa
     336            var blogAnswers = query.ToList();
     337            foreach (BlogPostAnswers answerClass in blogAnswers)
     338            {
     339                db.BlogPostAnswersObj.Remove(answerClass);
     340                db.SaveChanges();
     341
     342            }
     343
     344            // Отстранување на објектот ако е пронајден
    309345            db.BlogPostControllerObj.Remove(blogClass);
    310346            db.SaveChanges();
     347
     348            // Пренасочување на корисникот кон Index страницата
    311349            return RedirectToAction("Index");
    312350        }
    313        
     351
    314352        // GET: Customer/Delete/5
    315353        // GET: Customer/Delete/5
    316354        // GET: Customer/Delete/5
    317    
     355
    318356
    319357
  • PostgreSqlDotnetCore/Controllers/BlogPostAnswersController.cs

    ra850333 re90ba32  
    155155        public ActionResult Edit([Bind(include: "id,parent_id,reply,root_post,usersID")] BlogPostAnswers answerClass)
    156156        {
    157             if (ModelState.IsValid)
    158             {
    159                 db.Entry(answerClass).State = EntityState.Modified;
     157
     158            BlogPostAnswers answerClassDB = db.BlogPostAnswersObj.Find(answerClass.id);
     159            if (answerClassDB != null && !answerClassDB.reply.Equals(answerClass.reply))
     160            {
     161                answerClassDB.reply = answerClass.reply;
     162                answerClassDB.parent_id = answerClass.parent_id;
     163                db.Entry(answerClassDB).State = EntityState.Modified;
    160164                db.SaveChanges();
    161                 return RedirectToAction("Index");
    162             }
    163             return View(answerClass);
     165                //return RedirectToAction("Index");
     166                int id = answerClassDB.BlogPostConsultationid;
     167                return RedirectToAction("Details", "Blog", new { id });
     168            }
     169            return View(answerClassDB);
    164170        }
    165171
  • PostgreSqlDotnetCore/Controllers/PetCaresController.cs

    ra850333 re90ba32  
    5757        {
    5858            // check for permission
     59            bool isAuthenticated = User.Identity.IsAuthenticated;
    5960            UsersClass customerClass = await getCrrentUser();
    6061            // set if is authenticated
     
    7576
    7677                var userPetCares =
    77                     await query.ToListAsync<Pet_CaresClass>();
     78                    await query.Include(n => n.PetsClass).ToListAsync<Pet_CaresClass>();
    7879
    7980                return View(userPetCares);
     
    8182            else
    8283            {
    83                 return View(db.PetCaresObj.ToList());
     84                return View(db.PetCaresObj.Include(n => n.PetsClass).ToList());
    8485            }
    8586
     
    280281
    281282        // POST: Customer/Delete/5
     283        /*  [HttpPost, ActionName("Delete")]
     284          [ValidateAntiForgeryToken]
     285          public ActionResult DeleteConfirmed(int id)
     286          {
     287              Pet_CaresClass peClass = db.PetCaresObj.Find(id);
     288              db.PetCaresObj.Remove(peClass);
     289              db.SaveChanges();
     290              return RedirectToAction("Index");
     291          }
     292        */
     293
    282294        [HttpPost, ActionName("Delete")]
    283295        [ValidateAntiForgeryToken]
    284         public ActionResult DeleteConfirmed(int id)
    285         {
    286             Pet_CaresClass peClass = db.PetCaresObj.Find(id);
     296        public async Task<ActionResult> DeleteConfirmed(int id)
     297        {
     298            Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
     299            if (peClass == null)
     300            {
     301                return RedirectToAction("NotExist", "Error");
     302            }
    287303            db.PetCaresObj.Remove(peClass);
    288             db.SaveChanges();
     304            await db.SaveChangesAsync();
    289305            return RedirectToAction("Index");
    290306        }
  • PostgreSqlDotnetCore/Data/ApplicationDbContext.cs

    ra850333 re90ba32  
    6060            base.OnModelCreating(modelBuilder);
    6161
     62
     63            modelBuilder.Entity<BlogUsers>().HasNoKey().ToView("view_userss_consultationss");
     64            base.OnModelCreating(modelBuilder);
    6265            // Configure the relationship between VetCenter and CitiesClass
    63        
    6466
    65            
     67
     68
    6669            // ... model definition ...
    6770        }
     
    7376        public virtual DbSet<VetCenterWithCity> VetCentersWithCity { get; set; }
    7477
     78        public virtual DbSet<BlogUsers> BlogUsers { get; set; }
    7579        public virtual DbSet<BlogPostConsultation> BlogPostControllerObj { get; set; }
    7680        public virtual DbSet<RolesClass> RoleControllerObj { get; set; }
  • PostgreSqlDotnetCore/Models/Pet_CaresClass.cs

    ra850333 re90ba32  
    2222        public DateTime start_date { get; set; }
    2323
    24         // [ForeignKey("PetsClass")]
    25         // [Column("petid")]
     24        [ForeignKey("PetsClass")]
     25        [Column("pet_id")]
    2626        public int pet_id { get; set; }
    27         //public PetsClass PetsClass { get; set; }
     27        public PetsClass PetsClass { get; set; }
    2828
    2929
  • PostgreSqlDotnetCore/Views/Blog/Details.cshtml

    ra850333 re90ba32  
    7777
    7878                <td>
    79                     @Html.ActionLink("Edit", "Edit", "BlogPostAnswers", new { id = item.id }) |
    80                     @Html.ActionLink("Details", "Details", "BlogPostAnswers", new { id = item.id }) |
    81                     @Html.ActionLink("Delete", "Delete", "BlogPostAnswers", new { id = item.id })
    82 
     79                 
     80                    @Html.ActionLink("Details", "Details", "BlogPostAnswers", new { id = item.id })
    8381                    @Html.ActionLink("Create an Answer", "Create", "BlogPostAnswers", new { id = item.BlogPostConsultationid, parentId = item.id })
    8482                </td>
     83
     84                @if (item.usersid == ViewBag.CurrentUserId)
     85
     86                {
     87                    <td>
     88                       
     89                        @Html.ActionLink("Edit", "Edit", "BlogPostAnswers", new { id = item.id })
     90                        @Html.ActionLink("Delete", "Delete", "BlogPostAnswers", new { id = item.id })
     91                       
     92
     93                    </td>
     94                }
     95
     96
    8597            </tr>
    8698        }
     
    90102<p>
    91103    @Html.ActionLink("Create an Answer", "Create", "BlogPostAnswers", new { id = Model.id, parentId = Model.id })
    92     @Html.ActionLink("Edit", "Edit", new { id = Model.id }) |
    93104    @Html.ActionLink("Back to List", "Index")
    94105</p>
     106
     107
     108@if (Model.users_id == ViewBag.CurrentUserId)
     109
     110{
     111    <p>
     112        @Html.ActionLink("Edit", "Edit", new { id = Model.id })
     113       
     114
     115    </p>
     116}
  • PostgreSqlDotnetCore/Views/Blog/Index.cshtml

    ra850333 re90ba32  
    1 @model IEnumerable<PostgreSqlDotnetCore.Models.BlogPostConsultation>
     1@model IEnumerable<PostgreSqlDotnetCore.Models.BlogUsers>
    22
    33@{
     
    1414
    1515        <th>
    16             @Html.DisplayNameFor(model => model.date_askes)
     16            @Html.DisplayNameFor(model => model.id)
    1717        </th>
    1818        <th>
    19             @Html.DisplayNameFor(model => model.title)
     19            @Html.DisplayNameFor(model => model.user_name)
    2020        </th>
    2121        <th>
    22             @Html.DisplayNameFor(model => model.description)
     22            @Html.DisplayNameFor(model => model.user_email)
    2323        </th>
    2424        <th>
    25             @Html.DisplayNameFor(model => model.users_id)
     25            @Html.DisplayNameFor(model => model.consultation_title)
     26        </th>
     27        <th>
     28            @Html.DisplayNameFor(model => model.consultation_description)
     29        </th>
     30        <th>
     31            @Html.DisplayNameFor(model => model.consultation_date)
    2632        </th>
    2733        <th></th>
     
    3339        <tr>
    3440            <td>
    35                 @Html.DisplayFor(modelItem => item.date_askes)
     41                @Html.DisplayFor(modelItem => item.id)
    3642            </td>
    3743            <td>
    38                 @Html.DisplayFor(modelItem => item.title)
     44                @Html.DisplayFor(modelItem => item.user_name)
    3945            </td>
    40 
    4146            <td>
    42                 @Html.DisplayFor(modelItem => item.description)
     47                @Html.DisplayFor(model => item.user_email)
    4348            </td>
    44             <th>
    45                 @Html.DisplayFor(model => item.users_id)
    46             </th>
     49            <td>
     50                @Html.DisplayFor(modelItem => item.consultation_title)
     51            </td>
     52            <td>
     53                @Html.DisplayFor(modelItem => item.consultation_description)
     54            </td>
     55            <td>
     56                @Html.DisplayFor(modelItem => item.consultation_date)
     57            </td>
    4758
    4859            @*<td>
  • PostgreSqlDotnetCore/Views/PetCares/Index.cshtml

    ra850333 re90ba32  
    3131            @Html.DisplayNameFor(model => model.vetcentersid)
    3232        </th>
     33        <th>
     34            @Html.DisplayNameFor(model => model.PetsClass.name)
     35        </th>
    3336
    3437        <th></th>
     
    3639    </tr>
    3740
    38 @foreach (var item in Model) {
    39     <tr>
    40         <td>
    41             @Html.DisplayFor(modelItem => item.title)
    42         </td>
    43         <td>
    44             @Html.DisplayFor(modelItem => item.description)
    45         </td>
    46         <td>
    47             @Html.DisplayFor(modelItem => item.dateending)
    48         </td>
     41    @foreach (var item in Model)
     42    {
     43        <tr>
     44            <td>
     45                @Html.DisplayFor(modelItem => item.title)
     46            </td>
     47            <td>
     48                @Html.DisplayFor(modelItem => item.description)
     49            </td>
     50            <td>
     51                @Html.DisplayFor(modelItem => item.dateending)
     52            </td>
    4953            <td>
    5054                @Html.DisplayFor(modelItem => item.start_date)
    5155            </td>
    52         <td>
    53             @Html.DisplayFor(modelItem => item.usersid)
    54         </td>
    55         <td>
    56             @Html.DisplayFor(modelItem => item.vetcentersid)
    57         </td>
     56            <td>
     57                @Html.DisplayFor(modelItem => item.usersid)
     58            </td>
     59            <td>
     60                @Html.DisplayFor(modelItem => item.vetcentersid)
     61            </td>
     62            <td>
     63                @Html.DisplayFor(modelItem => item.PetsClass.name)
     64            </td>
    5865
    5966
    6067
    61         <td>
    62             @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
    63             @Html.ActionLink("Details", "Details", new { id = item.id }) |
    64             @Html.ActionLink("Delete", "Delete", new { id = item.id })
    65         </td>
    66     </tr>
    67 }
     68            <td>
     69                @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
     70                @Html.ActionLink("Details", "Details", new { id = item.id }) |
     71                @Html.ActionLink("Delete", "Delete", new { id = item.id })
     72            </td>
     73        </tr>
     74    }
    6875
    6976</table>
Note: See TracChangeset for help on using the changeset viewer.