Beispiel #1
0
        public async Task <TPrimaryKey> CreateAsync(BanEditDto dto)
        {
            if (dto == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound, $"{nameof(dto)} is null");
            }

            // user can't be banned twice for one post, so we will return existing ban id in this case
            Ban existingBan = null;

            if (dto.RelatedPost != null)
            {
                existingBan = await _context.Bans
                              .FirstOrDefaultAsync(ban => (!ban.IsDeleted) && (ban.RelatedPost.Id == dto.RelatedPost.Id));
            }

            if (existingBan != null)
            {
                throw new HttpResponseException(HttpStatusCode.Conflict, $"Ban already exists");
            }
            else
            {
                var entity = MapDtoToNewEntity <BanEditDto, Ban>(dto);
                entity.Category    = dto.Category == null ? null : _context.GetLocalOrAttach <Category>(dto.Category.Id);
                entity.RelatedPost = dto.RelatedPost == null ? null : _context.GetLocalOrAttach <Post>(dto.RelatedPost.Id);
                await _context.Bans.AddAsync(entity);

                await _context.SaveChangesAsync();

                return(entity.Id);
            }
        }
Beispiel #2
0
        public async Task EditAsync(BanEditDto dto)
        {
            var existingEntity = await _context.Bans.FirstOrDefaultAsync(ban => ban.Id == dto.Id);

            existingEntity.Category    = dto.Category == null ? null : _context.GetLocalOrAttach <Category>(dto.Category.Id);
            existingEntity.RelatedPost = dto.RelatedPost == null ? null : _context.GetLocalOrAttach <Post>(dto.RelatedPost.Id);
            MapDtoToExistingEntity(dto, existingEntity);
            await _context.SaveChangesAsync();
        }