public IHttpActionResult Get(int id)
        {
            Module module;

            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                module = _repo.AllIncluding(m => m.Name).FirstOrDefault(m => m.Id == id);
            }
            return(Ok <Module>(module));
        }
        public IHttpActionResult Get(int id)
        {
            Module module;
            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                module = _repo.AllIncluding(m => m.Name).FirstOrDefault(m => m.Id == id);

            }
            return Ok<Module>(module);
        }
        public IHttpActionResult Get(bool staticType)
        {
            List <ModuleViewModel> modules;

            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                modules = _repo.AllIncluding(m => m.Name)
                          .Where(m => !m.IsDeleted && m.ModuleType == Module.Type.Static).ToList()
                          .Select(m => m.ToModuleViewModel())
                          .ToList();
            }
            return(Ok <List <ModuleViewModel> >(modules));
        }
        public IHttpActionResult Get(string url)
        {
            Module module;
            string partialUrl = url.Split('/')[1];

            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                module = _repo.AllIncluding(m => m.Name)
                         .Where(m => !m.IsDeleted && (m.Url == url || m.Url == "/" + partialUrl))
                         .FirstOrDefault();
            }
            return(Ok <Module>(module));
        }
        public IHttpActionResult Get(bool staticType)
        {
            List<ModuleViewModel> modules;
            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                modules = _repo.AllIncluding(m => m.Name)
                    .Where(m => !m.IsDeleted && m.ModuleType == Module.Type.Static).ToList()
                    .Select(m => m.ToModuleViewModel())
                        .ToList();

            }
            return Ok<List<ModuleViewModel>>(modules);
        }
        public IHttpActionResult Get(string url)
        {
            Module module;
            string partialUrl = url.Split('/')[1];
            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                module = _repo.AllIncluding(m => m.Name)
                        .Where(m => !m.IsDeleted && (m.Url == url || m.Url == "/" + partialUrl))
                        .FirstOrDefault();

            }
            return Ok<Module>(module);
        }
        public IHttpActionResult Post(int id, List <ModuleContent> contents)
        {
            using (var _moduleContentRepo = new ModuleContentRepository(UnitOfWork))
            {
                using (var _repo = new ModuleRepository(UnitOfWork))
                {
                    // First update all the elements that need updating.
                    foreach (ModuleContent item in contents)
                    {
                        if (item.ModuleContentTranslations != null)
                        {
                            foreach (ModuleContentTranslation translation in item.ModuleContentTranslations)
                            {
                                if (translation.Id == default(int))
                                {
                                    translation.State = State.Added;
                                }
                                else
                                {
                                    translation.State = State.Modified;
                                }
                            }
                        }
                        _moduleContentRepo.InsertOrUpdateGraph(item);
                    }

                    // Then delete all the entities that are not there anymore.
                    var module = _repo.AllIncluding(m => m.ModuleContents)
                                 .FirstOrDefault(m => m.Id == id);

                    if (module != null)
                    {
                        // Delete the contents that needs to be deleted
                        List <ModuleContent> contentsToDelete = module.ModuleContents.Where(c => !contents.Select(i => i.Id).Contains(c.Id)).ToList();
                        contentsToDelete.ForEach(c => _moduleContentRepo.Delete(c.Id));
                    }
                    else
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
                    }

                    UnitOfWork.Save();
                }
            }


            return(Ok());
        }
        public IHttpActionResult Get()
        {
            List <Module> modules = new List <Module>();

            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                modules = _repo.AllIncluding(m => m.Name)
                          .Where(m => !m.IsDeleted)
                          .OrderBy(m => m.MenuPosition)
                          .ToList();
            }

            List <ModuleViewModel> moduleViewModels = modules.Select(m => m.ToModuleViewModel()).ToList();

            return(Ok <List <ModuleViewModel> >(moduleViewModels));
        }
        public IHttpActionResult Get()
        {
            List<Module> modules = new List<Module>();

            using(var _repo = new ModuleRepository(UnitOfWork))
            {
                modules = _repo.AllIncluding(m => m.Name)
                        .Where(m => !m.IsDeleted)
                        .OrderBy(m => m.MenuPosition)
                        .ToList();
            }

            List<ModuleViewModel> moduleViewModels = modules.Select(m => m.ToModuleViewModel()).ToList();

            return Ok<List<ModuleViewModel>>(moduleViewModels);
        }
        public IHttpActionResult Get(int id)
        {
            List <ModuleContent> moduleContents = new List <ModuleContent>();

            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                var module = _repo.AllIncluding(m => m.ModuleContents, m => m.ModuleContents.Select(mc => mc.ModuleContentTranslations))
                             .FirstOrDefault(m => m.Id == id);

                if (module != null)
                {
                    moduleContents = module.ModuleContents.Where(c => !c.IsDeleted).ToList();
                }

                return(Ok(moduleContents));
            }
        }
        public IHttpActionResult Get(int id)
        {
            List<ModuleContent> moduleContents = new List<ModuleContent>();

            using (var _repo = new ModuleRepository(UnitOfWork))
            {
                var module = _repo.AllIncluding(m => m.ModuleContents, m => m.ModuleContents.Select(mc => mc.ModuleContentTranslations))
                        .FirstOrDefault(m => m.Id == id);

                if(module != null)
                {
                    moduleContents = module.ModuleContents.Where(c => !c.IsDeleted).ToList();
                }

                return Ok(moduleContents);
            }
        }
        public IHttpActionResult Post(int id, List<ModuleContent> contents)
        {
            using(var _moduleContentRepo = new ModuleContentRepository(UnitOfWork))
            {
                using (var _repo = new ModuleRepository(UnitOfWork))
                {
                    // First update all the elements that need updating.
                    foreach (ModuleContent item in contents)
                    {
                        if(item.ModuleContentTranslations != null)
                        {
                            foreach (ModuleContentTranslation translation in item.ModuleContentTranslations)
                            {
                                if (translation.Id == default(int)) translation.State = State.Added;
                                else translation.State = State.Modified;
                            }
                        }
                        _moduleContentRepo.InsertOrUpdateGraph(item);
                    }

                    // Then delete all the entities that are not there anymore.
                    var module = _repo.AllIncluding(m => m.ModuleContents)
                            .FirstOrDefault(m => m.Id == id);

                    if (module != null)
                    {
                        // Delete the contents that needs to be deleted
                        List<ModuleContent> contentsToDelete = module.ModuleContents.Where(c => !contents.Select(i => i.Id).Contains(c.Id)).ToList();
                        contentsToDelete.ForEach(c => _moduleContentRepo.Delete(c.Id));
                    }
                    else throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));

                    UnitOfWork.Save();
                }
            }

            return Ok();
        }