public async Task <(bool, int)> TryAddPostToTreadAsync(Post post, string boardName, int treadId) { var tread = await _context.Treads.Include(t => t.Board) .FirstOrDefaultAsync(t => t.Board.Name == boardName && t.Id == treadId); if (tread == null) { return(false, 0); } PostEntity postEntity = post.ToEntity(); postEntity.Tread = tread; if (!postEntity.IsSage) { int count = _context.Entry(tread).Collection(t => t.Posts).Query().Count(); if (count < 500) { tread.TimeOfLastPost = postEntity.Time; } } _context.Posts.Add(postEntity); try { await _context.SaveChangesAsync(); } catch (Exception ex) { _logger.LogError(ex, "An error occurred while saving changes in the TryAddPostToTreadAsync method."); return(false, 0); } return(true, postEntity.Id); }
public static Post ToModel(this PostEntity post) { return(new(post.Id, post.PosterName, post.Message, post.Title, post.Time, post.Image?.ToModel(), post.IsOp, post.IsSage, post.TreadId, post.Tread?.Board?.Name)); }