public async Task <IActionResult> SetCategory(CategoryToReturn categoryToCreate) { var newCategory = _mapper.Map <Category> (categoryToCreate); _repo.Add(newCategory); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("Can not be created"); }
public async Task <IActionResult> Create([FromBody] LabelCreateDto dto) { if (dto.Name == null || dto.Name.Length == 0) { return(BadRequest(new { Message = "Label name cannot be empty." })); } var label = await _repo.FindLabelByName(dto.Name); if (label != null) { return(BadRequest(new { Message = "Label with this name already exists." })); } var newLabel = _mapper.Map <Label>(dto); _repo.Add(newLabel); if (await _repo.SaveAll()) { var labelToReturn = _mapper.Map <LabelDto>(newLabel); return(Ok(labelToReturn)); } return(BadRequest(new { Message = "Error creating the label." })); }
public async Task <IActionResult> Create([FromRoute] string projectId, [FromBody] PhaseCreateDto phaseCreateDto) { var userId = User.GetUserId(); var project = await _repo.GetProject(projectId, userId); if (project == null) { return(NotFound("Project not found.")); } if (project.Phases.FirstOrDefault(p => p.Name == phaseCreateDto.Name) != null) { return(BadRequest(new { message = "Phase already exists." })); } var phase = new Phase { Name = phaseCreateDto.Name, ProjectId = project.Id, //Project = project, Index = project.Phases.Count }; _repo.Add(phase); if (await _repo.SaveAll()) { var phaseListItem = _mapper.Map <PhaseDetailDto>(phase); return(Ok(new { phase = phaseListItem })); } return(BadRequest(new { message = "Unable to create the phase." })); }
// POST /api/comments public async Task <IActionResult> Create([FromBody] CommentCreateDto dto) { var userId = User.GetUserId(); var user = await _repo.GetUser(userId); //var issue = await _repo.GetIssue(dto.IssueId); var comment = new Comment { Text = dto.Text, CreatedAt = DateTime.Now, CreatedById = userId, IssueId = dto.IssueId, CreatedBy = user }; _repo.Add(comment); if (await _repo.SaveAll()) { var commentToReturn = _mapper.Map <CommentDto>(comment); return(Ok(commentToReturn)); } return(BadRequest()); }
public async Task <IActionResult> Create([FromBody] ProjectCreateDto dto) { if (!dto.IsValid()) { return(BadRequest(new { message = "Invalid information" })); } var userId = User.GetUserId(); var user = await _repo.GetUser(userId); if (user == null) { return(NotFound(new { message = "User not found" })); } var newProject = _mapper.Map <Project>(dto); newProject.CreatedById = userId; _repo.Add(newProject); if (await _repo.SaveAll()) { var projectUser = new ProjectUser { ProjectId = newProject.Id, UserId = userId, UserRole = Roles.MANAGER, LastActive = DateTime.Now }; _repo.Add(projectUser); if (await _repo.SaveAll()) { return(Ok(new { id = newProject.Id })); } return(BadRequest(new { message = "Error saving user data." })); } return(BadRequest(new { message = "Error creating the project." })); }
public async Task <IActionResult> AddProject(ProjectToCreateDto projectToCreate) { var newProject = _mapper.Map <Project>(projectToCreate); _repo.Add(newProject); if (await _repo.SaveAll()) { var projectToReturn = _mapper.Map <ProjectsForDetailed>(newProject); return(Ok(projectToReturn)); } throw new Exception("Project can't created"); }
public async Task <IActionResult> AddTicket(TicketToCreateDto ticketToCreate) { if (ticketToCreate.userId != null) { var userFromRepo = await _repo.GetUser(ticketToCreate.userId, false); if (userFromRepo == null) { return(BadRequest()); } if (userFromRepo.project.Id != ticketToCreate.projectId) { return(BadRequest()); } } var newTicket = _mapper.Map <Ticket>(ticketToCreate); newTicket.project = await _repo.GetProject(ticketToCreate.projectId); if (ticketToCreate.userId != null) { newTicket.User = await _repo.GetUser(ticketToCreate.userId, false); } if (ticketToCreate.CategoryId != 0) { newTicket.Status = await _repo.GetStatus(ticketToCreate.CategoryId); } if (ticketToCreate.StatusId != 0) { newTicket.Category = await _repo.GetCategory(ticketToCreate.CategoryId); } if (ticketToCreate.PriorityId != 0) { newTicket.Priority = await _repo.GetPriority(ticketToCreate.PriorityId); } _repo.Add(newTicket); if (await _repo.SaveAll()) { var ticketToReturn = _mapper.Map <TicketsForDetailed>(newTicket); return(Ok(ticketToReturn)); } throw new Exception("Ticket can't created"); }
public async Task <IActionResult> Create([FromBody] UserCreateDto dto) { var u = await _repo.GetUser(dto.Id); if (u != null) { return(Ok()); } var user = _mapper.Map <User>(dto); user.Avatar = ""; _repo.Add(user); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest()); }
public async Task <IActionResult> AddComment(CommentToCreateDto commentToCreate) { var commentForAdd = _mapper.Map <Comment>(commentToCreate); commentForAdd.Ticket = await _repo.GetTicket(commentToCreate.TicketId); commentForAdd.Commenter = await _repo.GetUser(commentToCreate.CommenterId, false); commentForAdd.Updated = commentToCreate.Created; _repo.Add(commentForAdd); if (await _repo.SaveAll()) { return(Ok(commentForAdd)); } throw new Exception("Commect is not posted"); }
public async Task <IActionResult> UploadAttachment([FromForm] AddAttachmentDto dto) { var userId = User.GetUserId(); if (dto.Attachment != null) { var attachment = dto.Attachment; var folderName = Path.Combine("Resources", "Attachments", dto.IssueId); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); var fileName = ContentDispositionHeaderValue.Parse(attachment.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); while (System.IO.File.Exists(fullPath)) { var fileNameNoExt = fileName.Split('.')[0]; var ext = fileName.Split('.')[1]; if (fileNameNoExt.Length > 8 && fileNameNoExt.Substring(fileNameNoExt.Length - 7, 4) == "Copy") { var copyNumber = int.Parse(fileNameNoExt[fileNameNoExt.Length - 2].ToString()); fileNameNoExt = $"{fileNameNoExt.Substring(0, fileNameNoExt.Length - 2)}{++copyNumber})"; } else { fileNameNoExt += "(Copy-1)"; } fileName = $"{fileNameNoExt}.{ext}"; fullPath = Path.Combine(pathToSave, fileName); } var dbPath = Path.Combine(folderName, fileName); if (!Directory.Exists(pathToSave)) { Directory.CreateDirectory(pathToSave); } using (var stream = new FileStream(fullPath, FileMode.Create)) { attachment.CopyTo(stream); } var newAttachment = new Attachment { Url = dbPath, CreatedById = userId, IssueId = dto.IssueId, Size = Convert.ToInt32(attachment.Length) }; _repo.Add(newAttachment); if (await _repo.SaveAll()) { newAttachment.CreatedBy = await _repo.GetUser(newAttachment.CreatedById); var attachmentToReturn = _mapper.Map <AttachmentDto>(newAttachment); return(Ok(attachmentToReturn)); } return(BadRequest()); } return(BadRequest()); }
public async Task <IActionResult> Create([FromBody] IssueCreateDto dto) { var userId = User.GetUserId(); var user = await _repo.GetUser(userId); if (user == null) { return(NotFound(new { message = "User not found" })); } var project = await _repo.GetProject(dto.ProjectId, userId); if (project == null) { return(NotFound(new { message = "Project not found." })); } var phase = project.Phases.FirstOrDefault(p => p.Id == dto.PhaseId); if (phase == null) { return(NotFound(new { message = "Phase not found." })); } var issue = _mapper.Map <Issue>(dto); issue.CreatedById = userId; issue.CreatedAt = DateTime.Today.ToShortDateString(); issue.PhaseId = dto.PhaseId; issue.Status = Status.TO_DO; _repo.Add(issue); if (dto.Labels != null) { foreach (var id in dto.Labels) { var label = await _repo.GetLabel(id); var il = new IssueLabel { IssueId = issue.Id, LabelId = label.Id, Issue = issue, Label = label }; issue.IssueLabels.Add(il); } } foreach (var id in dto.IssuedTo) { var iu = new IssueUser { IssueId = issue.Id, UserId = id }; issue.IssuedTo.Add(iu); } if (await _repo.SaveAll()) { var issueToReturn = _mapper.Map <IssueListItemDto>(issue); return(Ok(new { message = "Successfully created the issue", issue = issueToReturn })); } return(BadRequest(new { message = "Error saving the issue." })); }