Example #1
0
        public virtual async Task <ServiceResult <TEntity> > Delete(string id)
        {
            var response = new ServiceResult <TEntity>(User);
            var existing = await Repository.GetByIdAsync(id);

            if (existing is null)
            {
                NotFound();
                return(response);
            }

            try
            {
                await Repository.DeleteAsync(existing);

                response.AddSuccessMessage($"Successfully deleted {existing}");
                CreatedAtAction(nameof(Add), response);
            }
            catch (FormatException ex)
            {
                response.AddErrorMessage($"Supplied invalid id: {ex.Message}");
                BadRequest(response);
            }
            catch (Exception ex)
            {
                response.AddErrorMessage(ex.Message);
                BadRequest(response);
            }

            return(response);
        }
Example #2
0
        public virtual async Task <ServiceResult <TEntity> > Add(TEntity entity)
        {
            var response = new ServiceResult <TEntity>(User);

            try
            {
                response.Data = await Repository.CreateAsync(entity);

                response.AddSuccessMessage($"Successfully created {entity}");
                CreatedAtAction(nameof(Add), response);
            }
            catch (FormatException ex)
            {
                var msg = $"Supplied invalid id: {ex.Message} Please don't use any ID here - it will be created automatically.";
                response.AddErrorMessage(msg);
                BadRequest(response);
            }
            catch (Exception ex)
            {
                response.AddErrorMessage(ex.Message);
                BadRequest(response);
            }

            return(response);
        }
Example #3
0
        public virtual async Task <ServiceResult <TEntity> > Update(string id, TEntity entity)
        {
            var response = new ServiceResult <TEntity>(User);
            var existing = await Repository.GetByIdAsync(id);

            if (existing is null)
            {
                NotFound();
            }

            try
            {
                response.Data = await Repository.UpdateAsync(entity);

                response.AddSuccessMessage($"Successfully updated entity: {entity}");
                Ok(response);
            }
            catch (FormatException ex)
            {
                response.AddErrorMessage($"Supplied invalid id: {ex.Message}");
                BadRequest(response);
            }
            catch (Exception ex)
            {
                response.AddErrorMessage(ex.Message);
                BadRequest(response);
            }

            return(response);
        }
Example #4
0
        public async Task <IActionResult> GetTree()
        {
            var result = new ServiceResult <TreeDTO>();

            try
            {
                result.Data = await _scorpioCanOpenObjectRepository.GetTreeAsync();
            }
            catch (Exception ex)
            {
                result.AddErrorMessage(ex.Message);
                return(BadRequest(result));
            }

            return(Ok(result));
        }
Example #5
0
        public async Task <IActionResult> GetCanObject(string index, string subIndex)
        {
            var result = new ServiceResult <CanOpenObjectResponseDTO>();

            try
            {
                result.Data = await _scorpioCanOpenObjectRepository.GetCanOpenObjectAsync(index, subIndex);

                if (result.Data is null)
                {
                    return(NotFound(result));
                }
            }
            catch (Exception ex)
            {
                result.AddErrorMessage(ex.Message);
                return(BadRequest(result));
            }

            return(Ok(result));
        }
Example #6
0
        public IActionResult SendCanOpenObject(SendCanOpenObjectParam canOpenObjectParam)
        {
            var result = new ServiceResult <object>();

            try
            {
                _eventBus.Publish(new SendCanOpenObjectCommand
                {
                    Index    = canOpenObjectParam.Index,
                    SubIndex = canOpenObjectParam.SubIndex,
                    DataType = canOpenObjectParam.DataType,
                    Value    = canOpenObjectParam.Value
                });

                result.AddSuccessMessage("Successfully published");
                return(Ok(result));
            }
            catch (Exception ex)
            {
                result.AddErrorMessage(ex.Message);
                return(BadRequest(result));
            }
        }