Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 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));
 }