C#C
C#•2y ago•
26 replies
TrustyTea

Minimal API : Model / DTO Help

Hello! I'm new here and I'm looking for some help on my project. 🙂

I'm working on building a website for my portfolio. I've got a Blazor frontend, and I'm using a minimal API for my backend.

I have models for BlogPosts, Projects and Tags. I am also using DTOs for each. I decided to use DTOs to prevent cyclical relationships but I keep running into them.

I'm quite new to this side of .NET and using Databases. I've been at this for a while and having a hard time getting it to work how I'd like it to.

From my BlogEndpoints, I'd like my GET requests to get all my blogs from the database, which is what it does currently, but it shows me only the IDs of the Tags and Projects. I think showing just the ID for the Project is fine since I'll be able to use the id in a link using said ID. I'd like to have a List<Tag> Tags in each of my BlogPost so that I can just iterate through and do something like this on my Blazor end :

foreach(Tag tag in blogPost.Tags){
  <TagItem Data=tag/>
}


I'm sure there are better ways, but that's why I came here to ask for some advice. If I can figure out how to fix this on my Blog endpoint, I'll be able to fix it on my Project endpoint as well.

Here are the Model and DTO for my BlogPost, as well as my GET and POST request for my endpoint

namespace BackEndAPI.DTOs
{
    public class BlogPostDTO {
        [Required]
        public string Title { get; set; }
        [Required]
        public string Body { get; set; }
        [Required]
        public string Summary { get; set; }
        public int? ProjectId { get; set; }
        public List<int> TagIds { get; set; } = new List<int>();
    }
}


namespace BackEndAPI.Models
{
    public class BlogPost {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public string Body { get; set; }
        [Required]
        public string Summary { get; set; }
        [Required]
        public DateTime CreatedOn { get; set; } = DateTime.UtcNow;

        public int? ProjectId { get; set; }
        [JsonIgnore]
        public Project? Project { get; set; }
        [JsonIgnore]
        public List<Tag> Tags { get; set; } = new();
    }
}


private static async Task<IResult> GetBlogs(ApplicationDbContext db)
        {
            var blogPosts = await db.Blogs.Include(b => b.Tags).ToListAsync();
            return Results.Ok(blogPosts);
        }

        private static async Task<IResult> GetBlogById(int id, ApplicationDbContext db)
        {
            var blogPost = await db.Blogs.SingleOrDefaultAsync(b => b.Id == id);
            return Results.Ok(blogPost);
        }

        private static async Task<IResult> CreateBlog(BlogPostDTO dto, ApplicationDbContext db)
        {
            var tags = await db.Tags.Where(t => dto.TagIds.Contains(t.Id)).ToListAsync();
            var project = await db.Projects.Where(p => dto.ProjectId == p.Id).ToListAsync();
            var blogPost = new BlogPost
            {
                Title = dto.Title,
                Body = dto.Body,
                Summary = dto.Summary,
                ProjectId = dto.ProjectId,
                Project = project[0],
                Tags = tags,
            };

            db.Blogs.Add(blogPost);
            await db.SaveChangesAsync();
            return Results.Created($"/blogs/{blogPost.Id}", blogPost);
        }


Maybe DTOs aren't needed. Maybe I should do away with the List<Tag> Tags in my model and just use a List<int> TagIds instead and then ask the database what each Id corresponds to. I'm not sure which direction I should head.

Any and all feedback and help would be greatly appreciated 🙂
Was this page helpful?