public Result <bool> Edit(string email, string newName, string newAddress, DateTime newBirthDate) { var result = new Result <bool>(); var context = new CrowDoDbContext(); var updatedUser = context.Set <User>() .SingleOrDefault(u => u.Email == email); if (IsValidEmail(email) == false) { result.ErrorCode = 16; result.ErrorText = "Invalid email"; return(result); } if (updatedUser == null) { result.ErrorCode = 8; result.ErrorText = "No user found"; return(result); } updatedUser.Name = newName; updatedUser.Address = newAddress; updatedUser.BirthDate = newBirthDate; if (context.SaveChanges() < 1) { result.ErrorCode = 15; result.ErrorText = "An error occured while saving data"; return(result); } result.Data = true; return(result); }
public Result <bool> Delete(string email) { var context = new CrowDoDbContext(); var result = new Result <bool>(); var user = context.Set <User>() .Include(u => u.CreatedProjects) .SingleOrDefault(u => u.Email == email); if (IsValidEmail(email) == false) { result.ErrorCode = 14; result.ErrorText = "Invalid email"; return(result); } context.Remove(user); if (context.SaveChanges() < 1) { result.ErrorCode = 15; result.ErrorText = "An error occured while saving data"; return(result); } result.Data = true; return(result); }
public ReportService( CrowDoDbContext context, IUserService users, IProjectService projects) { context_ = context; user_ = users; project_ = projects; }
public Result <User> Create(string email, string name, string address, DateTime birthDate) { var context = new CrowDoDbContext(); var result = new Result <User>(); //Validations if (IsValidEmail(email) == false) { result.ErrorCode = 10; result.ErrorText = "Invalid email"; return(result); } if (string.IsNullOrWhiteSpace(name)) { result.ErrorCode = 11; result.ErrorText = "Invalid name"; return(result); } if (birthDate.AddYears(18) > DateTime.Now) { result.ErrorCode = 12; result.ErrorText = "Not permited"; return(result); } var existingEmail = context.Set <User>().Where(m => m.Email == email).Any(); // returns bool // if (existingEmail == true) { result.ErrorCode = 13; result.ErrorText = "An account with the same email already exists"; return(result); } var user = new User() { Email = email, Name = name, Address = address, BirthDate = birthDate, RegistrationDate = DateTime.Now }; context.Add(user); if (context.SaveChanges() < 1) // validation for Savechanges { result.ErrorCode = 13; result.ErrorText = "An error occured while saving data"; return(result); } result.Data = user; return(result); }
public Result <Project> PublishProject(string creatorEmail, string projectName , string category, string description, decimal projectGoal, DateTime creationDate, DateTime monthDuration, int estimatedMonthDuration) { var result = new Result <Project>(); if (IsValidEmail(creatorEmail) == false) { result.ErrorCode = 1; result.ErrorText = "Email is not valid"; return(result); } var context = new CrowDoDbContext(); var creator = context.Set <User>() .Where(c => c.Email == creatorEmail) .SingleOrDefault(); if (creator == null) { result.ErrorCode = 2; result.ErrorText = "User not registered"; return(result); } var project = new Project() { ProjectName = projectName, ProjectCategory = category, ProjectGoal = projectGoal, Description = description, CreationDate = creationDate, ExpirationDate = monthDuration, EstimatedDurationInMonths = DateTime.Now.AddMonths(estimatedMonthDuration), IsAvailable = true }; creator.CreatedProjects.Add(project); creator.CreatedProjectsCount++; if (context.SaveChanges() < 1) { result.ErrorCode = 7; result.ErrorText = "An error occurred while saving data"; return(result); } result.Data = project; return(result); }
public Result <bool> MonthlyReport(string ExcelFileName) { var context = new CrowDoDbContext(); var result = new Result <bool>(); var monthlyProjects = context.Set <Project>() .Include(p => p.RewardPackages) .Where(p => p.CreationDate.AddDays(30) >= DateTime.Today) .ToList(); XSSFWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet("Mysheet"); var row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue("ProjectId"); row.CreateCell(1).SetCellValue("ProjectName"); row.CreateCell(2).SetCellValue("Description"); row.CreateCell(3).SetCellValue("ProjectCategory"); row.CreateCell(4).SetCellValue("CreationDate"); row.CreateCell(5).SetCellValue("ExpirationDate"); row.CreateCell(6).SetCellValue("ProjectGoal"); row.CreateCell(7).SetCellValue("Funds"); row.CreateCell(8).SetCellValue("RewardPackages"); row.CreateCell(9).SetCellValue("IsAvailable"); row.CreateCell(10).SetCellValue("IsSuccessful"); row.CreateCell(11).SetCellValue("EstimatedDurationInMonths"); for (int i = 0; i < monthlyProjects.Count; i++) { row = sheet.CreateRow(i + 1); row.CreateCell(0).SetCellValue(monthlyProjects[i].ProjectId); row.CreateCell(1).SetCellValue(monthlyProjects[i].ProjectName); row.CreateCell(2).SetCellValue(monthlyProjects[i].Description); row.CreateCell(3).SetCellValue(monthlyProjects[i].ProjectCategory); row.CreateCell(4).SetCellValue(monthlyProjects[i].CreationDate.ToShortDateString()); row.CreateCell(5).SetCellValue(monthlyProjects[i].ExpirationDate.ToShortDateString()); row.CreateCell(6).SetCellValue(monthlyProjects[i].ProjectGoal.ToString()); row.CreateCell(7).SetCellValue(monthlyProjects[i].Funds.ToString()); row.CreateCell(8).SetCellValue(monthlyProjects[i].RewardPackages.Count.ToString()); row.CreateCell(9).SetCellValue(monthlyProjects[i].IsAvailable); row.CreateCell(10).SetCellValue(monthlyProjects[i].IsSuccessful); row.CreateCell(11).SetCellValue(monthlyProjects[i].EstimatedDurationInMonths); } using (var fs = new FileStream(ExcelFileName, FileMode.Create, FileAccess.Write)) { wb.Write(fs); } return(result); }
public Result <bool> EditProject(int userId, int projectId, string NewProjectName, string newProjectCategory, string description, decimal newProjectGoal, DateTime monthDuration, int estimatedMonthDuration) { var result = new Result <bool>(); var context = new CrowDoDbContext(); var updateProject = context.Set <Project>() .SingleOrDefault(b => b.ProjectId == projectId); var user = context.Set <User>() .Include(u => u.CreatedProjects) .SingleOrDefault(u => u.UserId == userId); if (!user.CreatedProjects.Contains(updateProject)) { result.ErrorCode = 15; result.ErrorText = "You don't have creator rights for this project"; return(result); } if (updateProject == null) { result.ErrorCode = 22; result.ErrorText = "No project was found"; return(result); } updateProject.ProjectName = NewProjectName; updateProject.ProjectCategory = newProjectCategory; updateProject.ProjectGoal = newProjectGoal; updateProject.Description = description; updateProject.ExpirationDate = monthDuration; updateProject.EstimatedDurationInMonths = DateTime.Now.AddMonths(estimatedMonthDuration); if (newProjectGoal <= updateProject.Funds) { updateProject.IsSuccessful = true; } if (context.SaveChanges() < 1) //** vaalidation for Savechanges : registration is ok or not { result.ErrorCode = 7; result.ErrorText = "No save"; return(result); } result.Data = true; return(result); }
public Result <List <Project> > GetPopularProjects() { var context = new CrowDoDbContext(); var result = new Result <List <Project> >(); var popularProjects = context.Set <Project>() .Where(p => p.IsAvailable == true) .OrderByDescending(p => p.Visits) .Take(5) .ToList(); result.Data = popularProjects; return(result); }
public Result <List <Project> > GetRecentProjects() { var context = new CrowDoDbContext(); var result = new Result <List <Project> >(); var recentProjects = context.Set <Project>() .Where(p => p.IsAvailable == true) .OrderByDescending(p => p.CreationDate) .Take(5) .ToList(); result.Data = recentProjects; return(result); }
public Result <List <User> > GetTopCreators() { var context = new CrowDoDbContext(); var result = new Result <List <User> >(); var topCreators = context.Set <User>() .OrderByDescending(u => u.CreatedProjectsCount) .Where(u => u.CreatedProjectsCount > 0) .Take(20) .ToList(); result.Data = topCreators; return(result); }
public Result <bool> DeleteProject(int userId, int projectId) { var result = new Result <bool>(); var context = new CrowDoDbContext(); var project = context.Set <Project>() .SingleOrDefault(p => p.ProjectId == projectId); var user = context.Set <User>() .Include(u => u.CreatedProjects) .SingleOrDefault(u => u.UserId == userId); if (!user.CreatedProjects.Contains(project)) { result.ErrorCode = 15; result.ErrorText = "You don't have creator rights for this project"; return(result); } if (project == null) { result.ErrorCode = 22; result.ErrorText = "No project was found"; return(result); } user.CreatedProjects.Remove(project); project.IsAvailable = false; user.CreatedProjectsCount--; if (context.SaveChanges() < 1) //** validation for Savechanges { result.ErrorCode = 7; result.ErrorText = "An error occurred while saving data"; return(result); } result.Data = true; return(result); }
public Result <List <Project> > GetFundedProjects() { var context = new CrowDoDbContext(); var result = new Result <List <Project> >(); var projectList = context.Set <Project>() .Where(p => p.IsSuccessful == true) .ToList(); if (projectList == null) { result.ErrorCode = 22; result.ErrorText = "No projects were found"; return(result); } result.Data = projectList; return(result); }
public Result <List <Project> > SearchByYear(int year) { var context = new CrowDoDbContext(); var projectList = context.Set <Project>() .Where(p => p.CreationDate.Year == year) .Where(p => p.IsAvailable == true) .ToList(); var result = new Result <List <Project> >(); if (projectList == null) { result.ErrorCode = 22; result.ErrorText = "No project was found"; return(result); } result.Data = projectList; return(result); }
public Result <List <Project> > SearchByCategory(string category) { var context = new CrowDoDbContext(); var projectList = context.Set <Project>() .Where(p => p.ProjectCategory == category) .Where(p => p.IsAvailable == true) .ToList(); var result = new Result <List <Project> >(); if (projectList == null) { result.ErrorCode = 22; result.ErrorText = "No project was found"; return(result); } result.Data = projectList; return(result); }
public Result <List <Project> > SearchByCreator(string email) { var context = new CrowDoDbContext(); var creator = context.Set <User>() .Include(c => c.CreatedProjects) .SingleOrDefault(p => p.Email == email); var result = new Result <List <Project> >(); if (creator == null) { result.ErrorCode = 24; result.ErrorText = "No creators found"; return(result); } result.Data = creator.CreatedProjects; return(result); }
public Result <List <Project> > SearchByText(string name, string category) { var context = new CrowDoDbContext(); var projectList = context.Set <Project>().Where(p => p.ProjectName.Contains(name)) .Where(c => c.ProjectCategory.Contains(category)).ToList(); var result = new Result <List <Project> > { Data = projectList }; if (result.Data == null) { result.ErrorCode = 22; result.ErrorText = "Poject not Found!"; } result.Data = projectList; return(result); }
public Result <bool> AddMultimediaFile(int userId, int projectId, string multimediaFile) { var result = new Result <bool>(); var context = new CrowDoDbContext(); var user = context.Set <User>() .Include(u => u.CreatedProjects) .SingleOrDefault(u => u.UserId == userId); var project = context.Set <Project>() .Include(p => p.MultimediaFiles) .SingleOrDefault(p => p.ProjectId == projectId); if (user.CreatedProjects.Contains(project)) { result.ErrorCode = 15; result.ErrorText = "You don't have creator rights for this project"; return(result); } project.MultimediaFiles.Add(new ProjectMedia() { FileName = multimediaFile }); if (context.SaveChanges() < 1) //** vaalidation for Savechanges : registration is ok or not { result.ErrorCode = 7; result.ErrorText = "An error occurred while saving data"; return(result); } result.Data = true; return(result); }
public Result <bool> DeadlineCheck() //Checks for expired Projects { var context = new CrowDoDbContext(); var result = new Result <bool>(); var expiredProjects = context.Set <Project>() .Where(p => p.ExpirationDate <= DateTime.Now) .ToList(); foreach (Project p in expiredProjects) { p.IsAvailable = false; } if (context.SaveChanges() < 1) { result.ErrorCode = 7; result.ErrorText = "There was an error communicating with the server"; return(result); } return(result); }
public Result <Project> GetProjectDetails(int projectId) { var context = new CrowDoDbContext(); var project = context.Set <Project>() .Include(p => p.RewardPackages) .SingleOrDefault(p => p.ProjectId == projectId); var result = new Result <Project>(); if (project == null) { result.ErrorCode = 22; result.ErrorText = "No project was found"; return(result); } if (project.IsAvailable == false) { result.ErrorCode = 29; result.ErrorText = "Project not available"; return(result); } project.Visits++; if (context.SaveChanges() < 1) { result.ErrorCode = 7; result.ErrorText = "There was an error communicating with the server"; return(result); } result.Data = project; return(result); }
public Result <string> GetFinancialProgress(int projectId) { var context = new CrowDoDbContext(); var result = new Result <string>(); var project = context.Set <Project>() .SingleOrDefault(i => i.ProjectId == projectId); if (project == null) { result.ErrorCode = 22; result.ErrorText = $"No project found with id :{projectId}"; return(result); } var financialProgress = (100 * project.Funds) / project.ProjectGoal; var p = Convert.ToInt32(financialProgress); //** It rounds up the number, it's marked successful when progress is above 99.5% result.Data = $"{p}%"; return(result); }
public Result <bool> AddRewardPackage(int userId, int projectId, string packageName, string rewardName, decimal price) { var result = new Result <bool>(); var context = new CrowDoDbContext(); var rewardPackage = new RewardPackage() { PackageName = packageName, RewardName = rewardName, Price = price }; var user = context.Set <User>() .Include(u => u.CreatedProjects) .SingleOrDefault(u => u.UserId == userId); var project = context.Set <Project>() .Include(p => p.RewardPackages) .SingleOrDefault(c => c.ProjectId == projectId); if (!user.CreatedProjects.Contains(project)) { result.ErrorCode = 15; result.ErrorText = "You don't have creator rights for this project"; return(result); } if (project == null) { result.ErrorCode = 22; result.ErrorText = "No project was found"; return(result); } if (price == 0.0M) { result.ErrorCode = 10; result.ErrorText = "You have to give a Price"; return(result); } if (price > project.ProjectGoal) { result.ErrorCode = 10; result.ErrorText = "Package price shouldn't exceed Goal amount!"; return(result); } if (packageName == null) { result.ErrorCode = 11; result.ErrorText = "You have to give a Name to PackageReward"; return(result); } if (rewardName == null) { result.ErrorCode = 12; result.ErrorText = "You have to give a Reward"; return(result); } project.RewardPackages.Add(rewardPackage); if (context.SaveChanges() < 1) { result.ErrorCode = 7; result.ErrorText = "An error occurred while saving data"; return(result); } result.Data = true; return(result); }
public Result <bool> FundProject(int userId, int projectId, int rewardPackageId) { var result = new Result <bool>(); var context = new CrowDoDbContext(); var backer = context.Set <User>() .SingleOrDefault(c => c.UserId == userId); var package = context.Set <RewardPackage>() .SingleOrDefault(c => c.RewardPackageId == rewardPackageId); var project = context.Set <Project>() .Include(p => p.RewardPackages) .SingleOrDefault(p => p.ProjectId == projectId); if (backer == null) { result.ErrorCode = 20; result.ErrorText = "User not registered"; return(result); } if (package == null) { result.ErrorCode = 19; result.ErrorText = "Reward package not found"; return(result); } if (project == null) { result.ErrorCode = 18; result.ErrorText = "Project not found"; return(result); } if (project.IsAvailable == false) { result.ErrorCode = 29; result.ErrorText = "Project not available"; return(result); } if (!project.RewardPackages.Contains(package)) { result.ErrorCode = 25; result.ErrorText = "The selected RewardPackage isn't available for this project"; return(result); } project.Funds += package.Price; if (project.Funds >= project.ProjectGoal) { project.IsSuccessful = true; } var backerReward = new LinkingTable { UserId = backer.UserId, RewardPackageId = package.RewardPackageId, ProjectId = projectId }; context.Add(backerReward); if (context.SaveChanges() < 1) { result.ErrorCode = 7; result.ErrorText = "An error occurred while saving data"; return(result); } result.Data = true; return(result); }