public async Task <ActionResult <TEditEntityDto> > Post([FromBody] TCreateEntityDto entityDto, [FromServices] RemboardContext context, [FromServices] IAuthorizationService authorizationService, [FromServices] CrudResourcePointControllerFactory <TEntity, TCreateEntityDto, TEditEntityDto, TFilterableEntity, TKey> controllerFactory)
        {
            var result = await authorizationService.AuthorizeAsync(User, typeof(TEntity), CrudOperations.Create);

            if (!result.Succeeded)
            {
                return(Forbid());
            }

            var validator = controllerFactory.GetCreateDtoValidator();

            var validationResult = await ValidateAsync(validator, entityDto);

            if (validationResult == null)
            {
                var operation  = controllerFactory.GetCrudOperation();
                var correctors = controllerFactory.GetCorrectors();
                var saved      = await operation.Post(entityDto, context, correctors);

                var producers = controllerFactory.GetAfterEntityCreateCommandProducersOrNull();

                await producers.SendToAll(entityDto, saved.entity.Id);

                return(Ok(saved.entityDto));
            }

            var errorResponse = new EntityResponse
            {
                Message = "Failed to save entity",
            };

            errorResponse.ValidationErrors.AddRange(validationResult);
            return(BadRequest(errorResponse));
        }
        public async Task <ActionResult <EntityEditFormModel> > GetEditSchema([FromServices] IAuthorizationService authorizationService, [FromServices] CrudResourcePointControllerFactory <TEntity, TCreateEntityDto, TEditEntityDto, TFilterableEntity, TKey> controllerFactory)
        {
            var updateResult = await authorizationService.AuthorizeAsync(User, typeof(TEntity), CrudOperations.Update);

            if (!updateResult.Succeeded)
            {
                return(Forbid());
            }

            var schemaProvider = controllerFactory.GetEntityFormSchemaProvider();
            var context        = new EntityEditSchemaProviderContext();

            var model = await schemaProvider.GetEditModelAsync(context);

            return(Ok(model));
        }
        public async Task <ActionResult <TEditEntityDto> > Get([FromRoute] string id, [FromServices] RemboardContext context, [FromServices] IAuthorizationService authorizationService, [FromServices] ILogger <CrudResourcePointController <TEntity, TCreateEntityDto, TEditEntityDto, TFilterableEntity, TKey> > logger, [FromServices] CrudResourcePointControllerFactory <TEntity, TCreateEntityDto, TEditEntityDto, TFilterableEntity, TKey> controllerFactory)
        {
            var result = await authorizationService.AuthorizeAsync(User, typeof(TEntity), CrudOperations.Read);

            if (!result.Succeeded)
            {
                return(Forbid());
            }

            var operation = controllerFactory.GetCrudOperation();

            try
            {
                var entityDto = await operation.Get(id, context, controllerFactory.GetMandatoryPredicateFactory());

                if (entityDto == null)
                {
                    var entityName = typeof(TEntity).Name;
                    logger.LogError("The entity '{entityName}' with {id} doesn`t exists ", id, entityName);
                    return(NotFound());
                }

                return(Ok(entityDto));
            }
            catch (WrongIdValueException)
            {
                var entityName = typeof(TEntity).Name;
                logger.LogError("Can`t parse {id} for {entityName}", id, entityName);
                return(NotFound());
            }
        }
        public async Task <ActionResult <TEditEntityDto> > Put([FromRoute] string id, [FromBody] TEditEntityDto entityDto, [FromServices] RemboardContext context, [FromServices] IAuthorizationService authorizationService, [FromServices] CrudResourcePointControllerFactory <TEntity, TCreateEntityDto, TEditEntityDto, TFilterableEntity, TKey> controllerFactory, [FromServices] ILogger <CrudResourcePointController <TEntity, TCreateEntityDto, TEditEntityDto, TFilterableEntity, TKey> > logger)
        {
            var result = await authorizationService.AuthorizeAsync(User, typeof(TEntity), CrudOperations.Create);

            if (!result.Succeeded)
            {
                return(Forbid());
            }

            var validator = controllerFactory.GetEditDtoValidator();

            var validationResult = await ValidateAsync(validator, entityDto);

            if (validationResult == null)
            {
                var predicates = controllerFactory.GetMandatoryPredicateFactory();
                var operation  = controllerFactory.GetCrudOperation();
                var correctors = controllerFactory.GetCorrectors();
                try
                {
                    var saved = await operation.Put(id, entityDto, context, predicates, correctors);

                    var producers = controllerFactory.GetAfterEntityEditCommandProducersOrNull();
                    await producers.SendToAll(entityDto, saved.entity.Id);

                    return(Ok(saved.entityDto));
                }
                catch (WrongIdValueException)
                {
                    var entityName = typeof(TEntity).Name;
                    logger.LogError("Can`t parse {id} for {entityName}", id, entityName);
                    return(NotFound());
                }
                catch (EntityNotFoundException)
                {
                    var entityName = typeof(TEntity).Name;
                    logger.LogError("The entity '{entityName}' with {id} doesn`t exists ", id, entityName);
                    return(NotFound());
                }
            }

            var errorResponse = new EntityResponse
            {
                Message = "Failed to save entity",
            };

            errorResponse.ValidationErrors.AddRange(validationResult);
            return(BadRequest(errorResponse));
        }