Exemplo n.º 1
0
        public BaseApiModule(string Route, IBaseModuleService BaseModuleService, IBaseFinder <TEntity> BaseFinder)
            : base(Route)
        {
            Get("/", parameters =>
            {
                var entities = BaseFinder.Get();

                return(BaseModuleService.RespondWithListOfEntitiesDTO <TEntity, TDTO>(this, entities));
            });

            Get("/{ID}", parameters =>
            {
                var entity = this.GetById <TEntity>((string)parameters.ID, BaseFinder);

                return(BaseModuleService.RespondWithEntitiyDTO <TEntity, TDTO>(this, entity));
            });
        }
Exemplo n.º 2
0
        public BaseApiModule(string route, IBaseModuleService baseModuleService, IBaseFinder <TEntity> baseFinder) : base(route)
        {
            Get("/", async ctx =>
            {
                var a        = ctx.Response;
                var entities = baseFinder.Get();

                await baseModuleService.RespondWithListOfEntitiesDTO <TEntity, TDTO>(ctx, entities);
            });

            Get("/{id:int}", async ctx =>
            {
                var entity = this.GetById <TEntity>(ctx.GetRouteData().As <int>("id"), baseFinder);

                await baseModuleService.RespondWithEntitiyDTO <TEntity, TDTO>(ctx, entity);
            });

            Post("/", async ctx =>
            {
                var entityDTO = await ctx.Request.Bind <TDTO>();
                var entity    = baseModuleService.ConvertToEntity <TDTO, TEntity>(entityDTO);

                BeforeInsertNewEntity?.Invoke(entity, entityDTO);

                baseFinder.Insert(entity);

                await baseModuleService.RespondWithEntitiyDTO <TEntity, TDTO>(ctx, entity);
            });

            Put("/{id:int}", async ctx =>
            {
                var entityDTO = await ctx.Request.Bind <TDTO>();

                var entity = this.GetById <TEntity>(ctx.GetRouteData().As <int>("id"), baseFinder);

                entity = baseModuleService.ConvertToEntity <TDTO, TEntity>(entityDTO, entity);

                baseFinder.Update(entity);

                await baseModuleService.RespondWithEntitiyDTO <TEntity, TDTO>(ctx, entity);
            });
        }