Beispiel #1
0
        public static T GetById <T>(this CarterModule Module, int Id, IBaseFinder <T> Finder)
            where T : Entity
        {
            T entity = Finder.GetById(Id);

            if (entity == null)
            {
                throw new HttpException(HttpStatusCode.BadRequest, $"Element with Id = {Id.ToString()} not found");
            }
            else
            {
                return(entity);
            }
        }
Beispiel #2
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));
            });
        }
Beispiel #3
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);
            });
        }
Beispiel #4
0
        public static T GetById <T>(this INancyModule Module, string Id, IBaseFinder <T> Finder)
            where T : Entity
        {
            int id;

            if (!int.TryParse(Id, out id))
            {
                throw new HttpException(HttpStatusCode.BadRequest, $"{Id} it is not an int");
            }

            T entity = Finder.GetById(id);

            if (entity == null)
            {
                throw new HttpException(HttpStatusCode.BadRequest, $"Element with Id = {id.ToString()} not found");
            }
            else
            {
                return(entity);
            }
        }