public async Task <IActionResult> Delete([FromRoute] string id, [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.Delete); if (!result.Succeeded) { return(Forbid()); } try { var operation = controllerFactory.GetCrudOperation(); var predicates = controllerFactory.GetMandatoryPredicateFactory(); await operation.Delete(id, context, predicates); } 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()); } return(Ok()); }
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 <PagedResult <TFilterableEntity> > > Get(FilterParameters filterParameters, [FromServices] RemboardContext context, [FromServices] IAuthorizationService authorizationService, [FromServices] ResourcePointControllerFactory <TEntity, TFilterableEntity, TKey> controllerFactory) { var result = await authorizationService.AuthorizeAsync(User, typeof(TEntity), CrudOperations.Read); if (!result.Succeeded) { return(Forbid()); } var filterableOperation = controllerFactory.GetFilterableOperation(); var predicateFactory = controllerFactory.GetMandatoryPredicateFactory(); var pagedResult = await filterableOperation.FilterAsync(context, predicateFactory, filterParameters); return(Ok(pagedResult)); }
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)); }
public UserService(RemboardContext context, ILogger <UserService> logger, UserManager <IdentityUser> userManager) { _context = context; _logger = logger; _userManager = userManager; }