Home > OS >  .net core 3.1 - How to create Post method can save data to many table but don't have foreign ke
.net core 3.1 - How to create Post method can save data to many table but don't have foreign ke

Time:12-25

I have 3 table is article, comment and image:

create table article (
   id int not null auto_increment,
   views int default null,
   title varchar(255) not null,
   status tinyint default 1,
   content text default null,
   created_date datetime default null,
   updated_date datetime default null,
   created_by int default null,
   updated_by int default null,
   primary key (id)
);

create table comment (
   id int not null auto_increment,
   content text default null,
   favorite int default null,
   created_date datetime default null,
   updated_date datetime default null,
   created_by int default null,
   updated_by int default null,
   article_id int default null,
   primary key (id)
);

create table image (
   id int not null auto_increment,
   id_obj int default null,
   url varchar(255) not null,
   thumbnail tinyint default null,
   type varchar(50) default null,
   primary key (id)
);

Here is my DB Diagram:

db diagram

Image table don't have foreign key with article table and comment table. Image table have two columns: id_obj and type. When I save id_obj = 2 and type = comment, it's mean record id 2 of comment table. On the contrary, id_obj = 3 and type = article, it's mean record id 3 of article table. How to create POST method for article that can add and save images to the image table? I tried to create but can only save data to DB and no image.

This is the Interface:

public interface IArticleService
{
    public Task<int> Create(ArticleRequest request);
}

This is Service:

public class ArticleService : IArticleService
{
        private readonly DbContext _context;

        public ArticleService(DbContext context)
        {
            _context = context;
        }

        public async Task<int> Create(ArticleRequest request)
        {
            var article = new Articles()
            {
                Views = request.Views,
                Title = request.Title, 
                Status = request.Status,
                Content = request.Content,
                CreatedDate = request.CreatedDate,
                UpdatedDate = request.UpdatedDate,
                CreatedBy = request.CreatedBy,
                UpdatedBy = request.UpdatedBy
            };

            _context.Articles.Add(article);

            return await _context.SaveChangesAsync();
        }
}

This is Post method:

[HttpPost]
public async Task<IActionResult> Create([FromBody] ArticleRequest request)
{
    var status = await _articleService.Create(request);            
    return Ok(status);
}

Looking forward to receiving an answer, or a similar post.

CodePudding user response:

There might be some details missing, but from the info you've provided, since your tables aren't "connected", so it is not enough to just say

public async Task<int> Create(ArticleRequest request)
    {
        var article = new Articles()
        {
            Views = request.Views,
            Title = request.Title, 
            Status = request.Status,
            Content = request.Content,
            CreatedDate = request.CreatedDate,
            UpdatedDate = request.UpdatedDate,
            CreatedBy = request.CreatedBy,
            UpdatedBy = request.UpdatedBy
        };

        _context.Articles.Add(article);

        return await _context.SaveChangesAsync();
    }

you would need something along the lines of

public async Task<int> Create(ArticleRequest request)
    {
        var article = new Articles()
        {
            Views = request.Views,
            Title = request.Title, 
            Status = request.Status,
            Content = request.Content,
            CreatedDate = request.CreatedDate,
            UpdatedDate = request.UpdatedDate,
            CreatedBy = request.CreatedBy,
            UpdatedBy = request.UpdatedBy
        };

        _context.Articles.Add(article);
        var image = new Image()
        {
            SomeImageProperty = request.someImageProperty,
            .
            .
            .
        };
        _context.Images.Add(image);

        return await _context.SaveChangesAsync();
    }
  • Related