public async Task <IResponseResult> GetScreens(string roleId, string menuId, string childId) { var screenDto = new List <ScreenDto>(); var role = await _roleUnitOfWork.Repository.FirstOrDefaultAsync(q => q.Id == roleId, include : source => source.Include(a => a.Menu), disableTracking : false); var dataAssigned = role.Menu.Select(q => q.MenuId).ToList(); if (string.IsNullOrEmpty(menuId) || menuId == "null") { var menuu = await _unitOfWork.Repository.FindAsync(q => !(q.IsStop ?? false) && !dataAssigned.Contains(q.Id), include : source => source.Include(a => a.Children).Include(b => b.Parent), disableTracking : false); var dtAll = menuu.Where(s => s.Children.Count == 0).Distinct().ToList(); foreach (var item in dtAll) { var screen = new ScreenDto() { Id = item.Id, ScreenNameAr = item.Parent == null ? item.ScreenNameAr : item.Parent.ScreenNameAr + ">" + item.ScreenNameAr, ScreenNameEn = item.Parent == null ? item.ScreenNameEn : item.Parent.ScreenNameEn + ">" + item.ScreenNameEn }; screenDto.Add(screen); } return(ResponseResult.GetRepositoryActionResult(screenDto, status: HttpStatusCode.OK, message: HttpStatusCode.OK.ToString())); } var parent = new Menus(); if (string.IsNullOrEmpty(childId) || childId == "null") { parent = await _unitOfWork.Repository.FirstOrDefaultAsync(q => !(q.IsStop ?? false) && q.Id == menuId, include : source => source.Include(a => a.Children)); } else { parent = await _unitOfWork.Repository.FirstOrDefaultAsync(q => !(q.IsStop ?? false) && q.Id == childId, include : source => source.Include(a => a.Children), disableTracking : false); } var idss = parent.Children.Select(q => q.Id).ToList(); _menuIdList.Add(parent.Id); _menuIdList.AddRange(idss); await GetChilds(idss); var MenuIdsPass = _menuIdList.Distinct().ToList(); var dataRes = await _unitOfWork.Repository.FindAsync(q => !(q.IsStop ?? false) && !dataAssigned.Contains(q.Id) && MenuIdsPass.Contains(q.Id), include : source => source.Include(a => a.Children).Include(b => b.Parent), disableTracking : false); var data = dataRes.Where(s => s.Children.Count == 0).Distinct().ToList(); foreach (var item in data) { var screen = new ScreenDto() { Id = item.Id, ScreenNameAr = item.Parent == null ? item.ScreenNameAr : item.Parent.ScreenNameAr + ">" + item.ScreenNameAr, ScreenNameEn = item.Parent == null ? item.ScreenNameEn : item.Parent.ScreenNameEn + ">" + item.ScreenNameEn }; screenDto.Add(screen); } return(ResponseResult.GetRepositoryActionResult(screenDto, status: HttpStatusCode.OK, message: HttpStatusCode.OK.ToString())); }
public async Task <IActionResult> Get(string project, Guid id) { if (!ModelState.IsValid || id == null || string.IsNullOrWhiteSpace(project)) { return(BadRequest("Invalid or missing parameters")); } GenericRepository <ScreenInBuild> screenInBuildRepo = _unitOfWork.ScreenInBuildRepository; ScreenInBuild sib = (await screenInBuildRepo.GetAsync(s => s.ScreenInBuildId == id && s.ProjectName.Equals(project), includeProperties: "Build")).FirstOrDefault(); if (sib == null) { return(NotFound()); } ScreenDto screen = new ScreenDto(sib); return(Ok(screen)); }
public async Task <IActionResult> Post(string project, string name, string locale, string build, [FromBody] CreateScreenDto screenData) { GenericRepository <Build> buildRepo = _unitOfWork.BuildRepository; Build buildEntity; string currentUser = User.Identity.Name; // Workaround for local readonly user if (!Authentication.LdapHelper.CanWrite(currentUser)) { return(Unauthorized()); } try { Guid buildId; if (!Guid.TryParse(build, out buildId)) { buildEntity = (await buildRepo.GetAsync(b => b.BuildName.Equals(build))).FirstOrDefault(); } else { buildEntity = buildRepo.GetByID(buildId); } } catch (Exception ex) { _logger.LogError(ex, "Error in screens controller"); return(BadRequest()); } if (buildEntity == null) { return(BadRequest($"Build with id {build} doesn't exist!")); } if (!ModelState.IsValid || buildEntity == null || string.IsNullOrWhiteSpace(project) || string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(locale) || screenData == null) { return(BadRequest("Invalid or missing parameters")); } GenericRepository <Project> projectRepo = _unitOfWork.ProjectRepository; Project projectEntity = projectRepo.GetByID(project); if (projectEntity == null) { return(BadRequest($"Project {project} doesn't exist!")); } GenericRepository <Locale> localeRepo = _unitOfWork.LocaleRepository; if (localeRepo.GetByID(locale) == null) { return(BadRequest($"Unknown locale {locale}!")); } GenericRepository <Screen> screenRepo = _unitOfWork.ScreenRepository; if (screenRepo.GetByID(name, project) == null) { Screen screen = new Screen() { Project = projectEntity, ScreenName = name }; screenRepo.Insert(screen); await _unitOfWork.SaveAsync(currentUser); } GenericRepository <ScreenInBuild> screenInBuildRepo = _unitOfWork.ScreenInBuildRepository; var existingScreen = screenInBuildRepo.Get(sr => sr.BuildId == buildEntity.Id && sr.ProjectName.Equals(project) && sr.LocaleCode.Equals(locale) && sr.ScreenName.Equals(name)).FirstOrDefault(); bool missingFile = false; if (existingScreen != null) { string existingScreenLocalPath = StorageHelper.GetScreenAbsPath(StorageHelper.GetScreenPath(project, locale, buildEntity.BuildName, name)); if (System.IO.File.Exists(existingScreenLocalPath)) { byte[] existingContent = System.IO.File.ReadAllBytes(existingScreenLocalPath); if (!screenData.Content.SequenceEqual(existingContent)) { return(StatusCode((int)HttpStatusCode.Conflict)); } else { return(StatusCode((int)HttpStatusCode.NoContent)); } } else { missingFile = true; } } string screenLocalPath = _storageHelper.StoreScreen(project, locale, buildEntity.BuildName, name, screenData.Content); ScreenDto newScreen; if (!missingFile) { ScreenInBuild newScreenInBuild = new ScreenInBuild() { ProjectName = project, ScreenName = name, BuildId = buildEntity.Id, LocaleCode = locale }; screenInBuildRepo.Insert(newScreenInBuild); await _unitOfWork.SaveAsync(currentUser); newScreen = new ScreenDto(newScreenInBuild); } else { newScreen = new ScreenDto(existingScreen); } return(CreatedAtRoute(routeName: "GetScreenRoute", routeValues: new { project = project, id = newScreen.Id }, value: newScreen)); }