public async Task <IActionResult> Create([FromBody] AnnotationSourceModel model) { _logger.LogDebug($"Create Annotation Source called."); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var source = await _annotationSourceService.GetBySourceUrl(model.SourceUrl); if (source == null) { _logger.LogDebug($"Source not found for source URL {model.SourceUrl}. Attempting to create source."); source = await _annotationSourceService.Add(model); } return(Ok(source)); } catch (Exception ex) { return(LogAndCreateErrorResponse(ex, $"An error occurred while creating annotation source.")); } }
public async Task <IActionResult> GetBySourceUrl([FromQuery] string sourceUrl) { _logger.LogDebug($"Get By Source called with source url: {sourceUrl}."); if (string.IsNullOrWhiteSpace(sourceUrl)) { return(BadRequest("A valid source url is required.")); } AnnotationSourceModel model = null; try { model = await _annotationSourceService.GetBySourceUrl(sourceUrl); if (model == null) { return(NotFound($"No annotation source with url {sourceUrl} was found.")); } _logger.LogDebug($"Returning annotation source for url: {sourceUrl}."); return(Ok(model)); } catch (Exception ex) { return(LogAndCreateErrorResponse(ex, $"An error occurred retrieving annotation source using url {sourceUrl}.")); } }
public async Task <AnnotationSourceModel> Add(string sourceUrl) { var model = new AnnotationSourceModel { SourceUrl = sourceUrl }; return(await Add(model)); }
public async Task <AnnotationSourceModel> Update(AnnotationSourceModel model) { var entity = await _annotationSources.SingleOrDefaultAsync(a => a.AnnotationSourceId == model.Id); model.PopulateEntity(entity); await _unitOfWork.SaveChangesAsync(); return(entity.ToModel()); }
public static AnnotationSource PopulateEntity(this AnnotationSourceModel model, AnnotationSource entity) { entity.RerumStorageUrl = model.RerumStorageUrl; entity.ImageHeight = model.ImageHeight; entity.ImageWidth = model.ImageWidth; entity.SourceUrl = model.SourceUrl; entity.CreatedDate = model.CreatedDate; entity.CreatedUser = model.CreatedUser; entity.UpdatedDate = model.UpdatedDate; entity.UpdatedUser = model.UpdatedUser; return(entity); }
public async Task <AnnotationSourceModel> Add(AnnotationSourceModel model) { var entity = model.ToEntity(); entity.CreatedDate = DateTime.UtcNow; entity.CreatedUser = "******"; entity.UpdatedUser = null; entity.UpdatedDate = null; await _annotationSources.AddAsync(entity); await _unitOfWork.SaveChangesAsync(); return(entity.ToModel()); }
public async Task <IActionResult> Update([FromBody] AnnotationSourceModel model) { _logger.LogDebug($"Update Annotation Source called for Id: {model.Id}"); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var source = await _annotationSourceService.Update(model); return(Ok(source)); } catch (Exception ex) { return(LogAndCreateErrorResponse(ex, $"An error occurred while updating annotation source for id {model.Id}.")); } }
public static AnnotationSourceModel ToModel(this AnnotationSource entity) { AnnotationSourceModel model = null; if (entity != null) { model = new AnnotationSourceModel { Id = entity.AnnotationSourceId, RerumStorageUrl = entity.RerumStorageUrl, ImageHeight = entity.ImageHeight, ImageWidth = entity.ImageWidth, SourceUrl = entity.SourceUrl, CreatedDate = entity.CreatedDate, CreatedUser = entity.CreatedUser, UpdatedDate = entity.UpdatedDate, UpdatedUser = entity.UpdatedUser, Targets = entity.Targets?.ToList().ConvertAll(t => t.ToModel()) }; } return(model); }
public static AnnotationSource ToEntity(this AnnotationSourceModel model, AnnotationSource entity = null) { AnnotationSource rt = null; if (model != null) { if (entity == null) { rt = new AnnotationSource() { AnnotationSourceId = model.Id, }; } else { rt = entity; } return(PopulateEntity(model, rt)); } return(rt); }