public async Task <Output> Handle(UpdateProjectParticipantsCommand request, CancellationToken cancellationToken) { try { var entity = _context.ProjectParticipants.Find(request.Id); if (entity == null) { return new Output { Status = false, ErrorMessage = "Entity dosn't exit." } } ; _mapper.Map(request, entity); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { return(new Output { Status = false, ErrorMessage = ex.Message }); throw; } }
public async Task <Output> Handle(UpdateIssueCommand request, CancellationToken cancellationToken) { try { var project = _context.Projects.Where(x => x.Id == request.ProjectId).Include(a => a.Issues).FirstOrDefault(); if (project == null) { return new Output { Status = false, ErrorMessage = "Project dosn't exist." } } ; var entity = _context.Issues.Find(request.Id); _mapper.Map(request, entity); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { return(new Output { Status = false, ErrorMessage = ex.Message }); throw; } }
public async Task <Output> Handle(DeleteIssueTypeCommand request, CancellationToken cancellationToken) { try { var issue = _context.IssueTypes.Find(request.Id); if (issue == null) { return new Output { Status = false, ErrorMessage = "issue dosn't exist." } } ; var hasIssues = _context.Issues.Any(o => o.IssueTypeId == issue.Id); if (hasIssues) { throw new DeleteFailureException(nameof(IsseTypes), request.Id, "There are existing Issues associated with this issueType."); } _context.IssueTypes.Remove(issue); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { //return new Output { Status = false, ErrorMessage = ex.Message }; throw; } }
public async Task <Output> Handle(CreateUserCommand request, CancellationToken cancellationToken) { try { var entity = _mapper.Map <User>(request); if (!string.IsNullOrEmpty(request.Password)) { entity.PasswordHash = SecurityHelper.Encrypt(request.Password); } _context.Users.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { return(new Output { Status = false, ErrorMessage = ex.Message }); throw; } }
public async Task <Output> Handle(DeleteIssueCommand request, CancellationToken cancellationToken) { try { var issue = _context.Issues.Find(request.Id); if (issue == null) { return new Output { Status = false, ErrorMessage = "issue dosn't exist." } } ; _context.Issues.Remove(issue); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { //return new Output { Status = false, ErrorMessage = ex.Message }; throw; } }
public static async Task <T> CreateEntity <T>(IIssueTrackerDbContext context, T entity) where T : class { // insert initial entity entity = context.Add(entity).Entity; await context.SaveChangesAsync(CancellationToken.None); return(entity); }
public async Task <Output> Handle(DeleteProjectParticipantsCommand request, CancellationToken cancellationToken) { try { var project = _context.Projects.Find(request.ProjectId); if (project == null) { return new Output { Status = false, ErrorMessage = "Project dosn't exist." } } ; if (project.OwnerId != request.OwnerId) { return new Output { Status = false, ErrorMessage = "You aren't not owner of this project!." } } ; foreach (var item in request.ParticipantEmails) { var user = _context.Users.Where(a => a.Email == item).FirstOrDefault(); if (user != null) { var entity = _context.ProjectParticipants.Where(a => a.ParticipantId == user.Id && a.ProjectId == request.ProjectId).FirstOrDefault(); if (entity != null) { _context.ProjectParticipants.Remove(entity); } } } await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { return(new Output { Status = false, ErrorMessage = ex.Message }); throw; } }
public async Task <Output> Handle(DeleteProjectCommand request, CancellationToken cancellationToken) { try { var project = _context.Projects.Find(request.Id); if (project == null) { return new Output { Status = false, ErrorMessage = "Project dosn't exist." } } ; if (project.OwnerId != request.OwnerId) { return new Output { Status = false, ErrorMessage = "You aren't not owner of this project!." } } ; var hasProjectParticipants = _context.ProjectParticipants.Any(o => o.ProjectId == project.Id); if (hasProjectParticipants) { throw new DeleteFailureException(nameof(Projects), request.Id, "There are existing Participants associated with this Project."); } var hasIssues = _context.Issues.Any(o => o.ProjectId == project.Id); if (hasIssues) { throw new DeleteFailureException(nameof(Projects), request.Id, "There are existing Issues associated with this Project."); } _context.Projects.Remove(project); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { //return new Output { Status = false, ErrorMessage = ex.Message }; throw; } }
public async Task <Output> Handle(CreateProjectCommand request, CancellationToken cancellationToken) { try { var user = _context.Users.Find(request.OwnerId); if (user == null) { return new Output { Status = false, ErrorMessage = "This owner dosn't exist in participants!." } } ; var entity = _mapper.Map <Project>(request); _context.Projects.Add(entity); //_context.ProjectParticipants.Add(new Domain.Entities.ProjectParticipants { // ParticipantId = request.OwnerId, // ProjectId = entity.Id //}); await _context.SaveChangesAsync(cancellationToken); await _mediator.Send(new CreateProjectParticipantsCommand { OwnerId = request.OwnerId, ProjectId = entity.Id, ParticipantEmails = new List <string>() { user.Email } }); return(new Output { Status = true }); } catch (System.Exception ex) { return(new Output { Status = false, ErrorMessage = ex.Message }); throw; } }
public async Task <Output> Handle(CreateIssueTypeCommand request, CancellationToken cancellationToken) { try { var entity = _mapper.Map <IssueType>(request); _context.IssueTypes.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(new Output { Status = true }); } catch (System.Exception ex) { return(new Output { Status = false, ErrorMessage = ex.Message }); throw; } }