Skip to content

Commit

Permalink
fix: correct entity loading for posts from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
anasinik committed Jul 4, 2024
1 parent aa52293 commit abbcac0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 5 additions & 4 deletions AdoptionAgency/Backend/Domain/Model/Post/Post.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using AdoptionAgency.Backend.Domain.Model.Common;
using AdoptionAgency.Backend.Domain.Model.Person.Member;
namespace AdoptionAgency.Backend.Domain.Model.Post
{
public class Post
{
public int Id { get; set; }
public List<Picture> Pictures { get; set; } = new List<Picture>();
public Animal.Animal Animal { get; set; } = default!;
public Member Member { get; set; } = default!;
public int AnimalId { get; set; }
public Person.Person Person { get; set; } = default!;
public int PersonId { get; set; }
public Status Status { get; set; } = default!;
public string Description { get; set; } = default!;
public Post(){}
public Post(int id, List<Picture> pictures, Animal.Animal animal, Member member, Status status, string description)
public Post(int id, List<Picture> pictures, Animal.Animal animal,Person.Person person, Status status, string description)
{
Id = id;
Pictures = pictures;
Animal = animal;
Member = member;
Person = person;
Status = status;
Description = description;
}
Expand Down
9 changes: 9 additions & 0 deletions AdoptionAgency/Backend/Repositories/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<AdoptionRequest>().ToTable(nameof(AdoptionRequest));

modelBuilder.Entity<Post>().ToTable(nameof(Post));
modelBuilder.Entity<Post>()
.HasOne(p => p.Animal)
.WithMany()
.HasForeignKey(p => p.AnimalId);

modelBuilder.Entity<Post>()
.HasOne(p => p.Person)
.WithMany()
.HasForeignKey(p => p.PersonId);
modelBuilder.Entity<Picture>().ToTable(nameof(Picture));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public Post Get(int id)

public List<Post> GetAll()
{
return _context.Post.ToList();
return _context.Post.Include(p => p.Animal)
.Include(p => p.Person)
.Include(p => p.Pictures)
.ToList();
}

public void Update(Post post)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AdoptionAgency.Backend.Domain.Model.Animal;
using AdoptionAgency.Backend.Domain.Model.Common;
using AdoptionAgency.Backend.Domain.Model.Person.Member;
using AdoptionAgency.Backend.Domain.Model.Person;
using AdoptionAgency.Backend.Domain.Model.Post;
using AdoptionAgency.Frontend.View.Common;
using AdoptionAgency.Frontend.ViewModel;
Expand All @@ -15,7 +15,7 @@ public PostViewModel() { }
public List<Picture> Pictures { get; set; } = new List<Picture>();
public ImageDisplay ImageDisplays { get; set; }
public Animal Animal { get; set; }
public Member Member { get; set; }
public Person Person { get; set; }
public Status Status { get; set; }
public string IconPath { get; set; }

Expand Down Expand Up @@ -63,15 +63,15 @@ public bool IsValid

public Post ToPost()
{
return new Post(Id, Pictures, Animal, Member, Status, Description);
return new Post(Id, Pictures, Animal, Person, Status, Description);
}

public PostViewModel(Post post)
{
Id = post.Id;
Pictures = post.Pictures;
Animal = post.Animal;
Member = post.Member;
Person = post.Person;
Status = post.Status;
Description = post.Description;
ImageDisplays = new(post.Pictures);
Expand Down

0 comments on commit abbcac0

Please sign in to comment.