private async Task <AnnotationModel> AddAnnotation(AnnotationModel model) { var target = model.Target?.ToEntity(); if (target != null && target.AnnotationTargetId < 1) { await _annotationTargets.AddAsync(target); } var entity = model.ToEntity(); entity.AnnotationTargetId = target.AnnotationTargetId; entity.CreatedDate = DateTime.UtcNow; entity.CreatedUserId = model.CreatedUserId; entity.GroupId = model.GroupId; await _annotations.AddAsync(entity); await _unitOfWork.SaveChangesAsync(); var added = await _annotations .Include(a => a.CreatedUser) .Include(a => a.Target) .Include(a => a.Group) .Include(a => a.License) .Include(a => a.AnnotationType) .Include(a => a.Tags).ThenInclude(at => at.Tag) .SingleOrDefaultAsync(a => a.AnnotationId == entity.AnnotationId); return(added.ToModel()); }
private async Task <AnnotationModel> UpdateAnnotation(AnnotationModel model) { var entity = await _annotations .Include(a => a.CreatedUser) .Include(a => a.Target) .Include(a => a.Group) .Include(a => a.License) .Include(a => a.AnnotationType) .Include(a => a.Tags).ThenInclude(at => at.Tag) .FirstOrDefaultAsync(a => a.AnnotationId == model.Id); if (entity != null) { entity = model.ToEntity(entity); entity.UpdatedDate = DateTime.UtcNow; await _unitOfWork.SaveChangesAsync(); var updated = entity.ToModel(); return(updated); } else { throw new NotFoundException($"Annotation with id {model.Id} not found."); } }