Пример #1
0
        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."));
            }
        }
Пример #2
0
        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}."));
            }
        }
Пример #3
0
        public async Task <AnnotationSourceModel> Add(string sourceUrl)
        {
            var model = new AnnotationSourceModel {
                SourceUrl = sourceUrl
            };

            return(await Add(model));
        }
Пример #4
0
        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());
        }
Пример #5
0
 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);
 }
Пример #6
0
        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());
        }
Пример #7
0
        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}."));
            }
        }
Пример #8
0
        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);
        }
Пример #9
0
        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);
        }