How to define model property for master detail - c#

I have a post model that for every post it's need to define category and tag , and from category or tag I wanna to reach all of posts that have that category of tag.
this is my blog model
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public string Summary { get; set; }
public DateTime CreationDate { get; set; }
public string UrlSlug { get; set; }
public Category Category { get; set; }
public Tag Tag { get; set; }
}
and this is Category:
public class Category
{
public Guid Id { get; set; }
public string Name { get; set; }
public string UrlSlug { get; set; }
public string Description { get; set; }
public DateTime CreationDate { get; set; }
public IList<Post> Posts { get; set; }
}
and finally this tag model:
public class Tag
{
public Guid Id { get; set; }
public string Name { get; set; }
public string UrlSlug { get; set; }
public string Description { get; set; }
public DateTime CreationDate { get; set; }
public IList<Post> Posts { get; set; }
}
I just want to know what I've done in designing model is right or not ??

You dont need the lists in the model ( remove public IList Posts { get; set; }
First create two get actions in the controller that takes tag ID, and the other`category ID. Inside the this actions get all the posts and populate your View ( detail view)
public ActionResult PostsByCategoryID(Guid Id)
{
List<post> posts = ///get them by id
return View(posts) ; //the view take list and displays the posts
}

Related

EF core how to make relations using array?

I have some entities
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Guid[] TagIds { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public string Id { get; set; }
public string Name { get; set; }
}
How to build relations if posts.tag_ids is uuid[]?
I want to use
_dbContext.Posts.Include(x => x.Tags)
Expecting SQL
select * from posts p
left join tags t on t.id = any(p.tag_ids)
This
public Guid[] TagIds { get; set; }
is not how relational databases model relationships. If you want to relate the entities, try this instead:
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public string Id { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts { get; set; }
}
It will create a linking table behind the scenes.

Query with four tables in ASP.net using lambda expression

I have a web page that allows user to sign petitions, in the user detail page I want to display the petitions the user has signed, including the petition image. The image is specific to the category of the petition.
I have a category model that holds the image
public class Category
{
public int CategoryID { get; set; }
public string Name { get; set;}
public string catImage { get; set; }
public ICollection<Cause> Causes { get; set; }
}
Then I have a cause model that links to the Category through CategoryID
public class Cause
{
public int CauseID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int UserID { get; set; }
public string createdBy { get; set; }
public DateTime createdOn { get; set; }
public int Target { get; set; }
public int CategoryID { get; set; }
public virtual ICollection<Signature> Signatures { get; set; }
public Category Category { get; set; }
}
Then a signature model that links the Cause to the User
public class Signature
{
public int SignatureID { get; set; }
public int CauseID { get; set; }
public int UserID { get; set; }
public DateTime signedOn { get; set; }
public virtual Cause Cause { get; set; }
public virtual User User { get; set; }
}
And finally a user model
public class User
{
public int ID { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string UserImage { get; set; }
public Role Role { get; set; }
public virtual ICollection<Signature> Signatures { get; set; }
}
On the user details page I am trying to display a table with a list of all the petitions the user has signed. I have been able to show the name and the date shown, but not the images.
I would really appreciated any help.
Thanks

C# mongodb model like Facebook

I'm working on a project where the MongoDB model will be similar to Facebook. So we all know how FB works, a user "likes" a band/company page, and that user will see all the posts from that page.
Is the below model how I should design this?
If a Page has million likes, then each Post will have a million sub documents of Like. That does not seem right, there must be a better way that I cant think of.
Thanks.
public class Person
{
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Page
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public List<Like> PersonLikes { get; set; }
}
public class Like
{
public ObjectId Id { get; set; }
public ObjectId UserId { get; set; }
public DateTime DateLiked { get; set; }
}
public class Post
{
public ObjectId Id { get; set; }
public ObjectId PageId { get; set; }
public string Message { get; set; }
public List<Like> PersonLikes { get; set; }
}
My take assuming you only want to track likes
public class Page
{
public ObjectId Id { get; set; }
public DateTimeOffset Date { get; set; }
public string Name { get; set; }
public int NumberOfLikes { get; set; }
}
public class Post
{
public ObjectId Id { get; set; }
public ObjectId PageId { get; set; }
public DateTimeOffset Date { get; set; }
public string Message { get; set; }
public int NumberOfLikes { get; set; }
}
I would then queue the Reaction (Like or Dislike) for insertion, "sentiment" information doesn't have to be stored in real time, does it? These are not medications, bank transactions, etc.
public class Like
{
public ObjectId Id { get; set; }
public ObjectId ParentId { get; set;}
public ObjectId UserId { get; set; }
public DateTimeOffset Date { get; set; }
}
Queue where? to a collection of Likes. Why not part of the page or post? Because if a post goes viral (as you said even though the majority won't), you may end up with a 1,000,000 likes. Who is going to browse this information other than an analytic engine?
You also have to ensure a user can only express their reaction only once per item.
The post only exists on one page so it´s the page that should own the post, not the post owning the page.
public class Person
{
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Page
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public List<Like> PersonLikes { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public ObjectId Id { get; set; }
public string Message { get; set; }
public List<Like> Likes { get; set; }
}
public class Like
{
public ObjectId Id { get; set; }
public ObjectId UserId { get; set; }
public DateTime DateLiked { get; set; }
}

Where to add method prototype in case of accessing data from 3 related models in repository pattern

I'm trying to create an ASP.NET MVC application using repostory pattern and dependency injection as design pattern. I have 3 related models
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string TagName { get; set; }
public string TagColor { get; set; }
public string ImageUrl { get; set; }
[ForeignKey("CategoryId")]
public virtual ICollection<SubCategory> SubCategories { get; set; }
}
public class SubCategory
{
public int SubCategoryId { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public string TagName { get; set; }
public string TagColor { get; set; }
public string ImageUrl { get; set; }
[ForeignKey("SubCategoryId")]
public virtual ICollection<SubSubCategory> SubSubCategories { get; set; }
}
public class SubSubCategory
{
public int SubSubCategoryId { get; set; }
public int SubCategoryId { get; set; }
public string Name { get; set; }
public string TagName { get; set; }
public string TagColor { get; set; }
public string ImageUrl { get; set; }
}
Now I want to define a method that will return a JSON object of all the three related models containing the data will be containing Navigation Menu(Category), SubCategories under each Category and SubSubCategory as MenuItems under each SubCategory. So How do I define repository and the method prototype ?
Also if you can suggest me any method that can do all that what I've just explained earlier using Entityframework I will be grateful to you. Thank you
foreach (Category category in context.Categories)
{
foreach (SubCategory subCategory in category.SubCategories)
{
foreach (SubSubCategory subSubCategory in subCategory.SubSubCategories)
{
}
}
}
This is what I'm doing to get all related data. If there is any good way to get all related data please let me know.
I suggest using one class instead of three different classes.
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public string TagName { get; set; }
public string TagColor { get; set; }
public string ImageUrl { get; set; }
[ForeignKey("CategoryId")]
public virtual ICollection<Category> SubCategories { get; set; }
}
This way every operation on all categories can be done with a simple recursive method.
private void SomeOperation(ICollection<Category> list)
{
foreach (Category category in list)
{
//Do something
SomeOperation(category.SubCategories);
}
}
I hope it turns out to be helpful.

Mapping JSON data to model and back in MVC 4

Pardon any misspeaking I may do. I am just learning asp.net c#, OOP, and MVC 4.
I am using the Annotator plugin on my site and I am trying to store and retrieve the results from a data base. When a new annotation is created it sends information like this to the controller.
{
"text":"asdas sd a dasd as dasd",
"ranges":
[{
"start":"/div/p[3]",
"startOffset":195,
"end":"/div/p[3]",
"endOffset":532
}],
"quote":"Some quote that was selected.",
"UserID":"1",
"ProjectDocID":"1"
}
Now, I hoped something like this would load all the data into a nice easy object.
// GET: /Annotation/Create
public ActionResult Create(Comment comment)
{
db.Comments.Add(comment);
db.SaveChanges();
return Json(comment);
}
The model I had looked like this (and I am change it however it needs to be changed).
public class Comment
{
[Key]
public int CommentID { get; set; }
public int ProjectDocID { get; set; }
public int UserID { get; set; }
public string text { get; set; }
public string Annotation { get; set; }
public string quote { get; set; }
public string start { get; set; }
public string end { get; set; }
public string startOffset { get; set; }
public string endOffset { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<CommentVote> CommentVote { get; set; }
public virtual ICollection<CommentReply> CommentReply { get; set; }
public ProjectDoc ProjectDoc { get; set; }
public virtual User User { get; set; }
}
What do I need to do to get the data from the server and map it to the model. I also need to be able to send it back in the format at the top so the plugin understands it when it asks for it from the Details action in the controller.
Not sure if this is the right way to do it or not, but it lets me store it in the database and that's all that matter for the moment. I updated my model as follows.
public class Comment
{
[Key]
public int CommentID { get; set; }
public int ProjectDocID { get; set; }
public int UserID { get; set; }
public string text { get; set; }
public string quote { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<CommentVote> CommentVote { get; set; }
public virtual ICollection<CommentReply> CommentReply { get; set; }
public ProjectDoc ProjectDoc { get; set; }
public virtual User User { get; set; }
public List<ranges> ranges { get; set; }
}
public class ranges {
public int ID { get; set; }
public string start { get; set; }
public string end { get; set; }
public string startOffset { get; set; }
public string endOffset { get; set; }
}
This matches the JSON object I am sending in to the controller with Ajax. Once it matches exactly the controller action above works.

Categories