public async Task <IActionResult> PatchTicket(int id, [FromBody] TicketForUpdateDto ticketForUpdateDto) { string userIdFromClaim = User.FindFirst(ClaimTypes.NameIdentifier).Value; if (!int.TryParse(userIdFromClaim, out int userId)) { return(BadRequest("Can not parse user id")); } try { Ticket ticket = await repo.PatchTicket(userId, id, ticketForUpdateDto.Description); if (ticket != null) { return(Ok(mapper.Map <TicketForReturnDto>(ticket))); } else { return(BadRequest("Failed to update the ticket")); } } catch (Exception e) { return(BadRequest(e.Message)); } }
public IActionResult Update(int id, [FromBody] TicketForUpdateDto ticketData) { if (ticketData == null) { return(BadRequest()); } var ticket = _ticketRepository.Query(id); if (ticket == null) { return(NotFound()); } ticket.Title = ticketData.Title == null ? ticket.Title : ticketData.Title; ticket.Description = ticketData.Description == null ? ticket.Description : ticketData.Description; ticket.Status = ticketData.Status == null ? ticket.Status : ticketData.Status; _ticketRepository.Update(ticket); if (!_ticketRepository.Save()) { return(BadRequest()); } return(new NoContentResult()); }
public async Task <IActionResult> UpdateTicket(Guid id, [FromBody] TicketForUpdateDto ticketToUpdate) { if (ticketToUpdate == null) { _logger.LogError("Ticket object sent from client is null."); return(BadRequest("Empty Ticket Cannot Be Created")); } if (!ModelState.IsValid) { _logger.LogError("Invalid model state for the Ticket"); return(UnprocessableEntity(ModelState)); } var originalTicket = await _repo.Ticket.GetTicket(id); if (originalTicket == null) { _logger.LogError("Invalid ticket id"); return(BadRequest("The ticket that you are trying to update doesnot exist")); } //getting usertickets from database and check the owner var usersticketFromDatabase = await _repo.UserTicket.GetUserTicket(id); var isOwner = CheckTicketOwner.IsTicketOwner(User, usersticketFromDatabase); if (!isOwner) { return(Unauthorized("Sorry! You cannot modify this ticket.")); } //updating the database so the previous record gets deleted in database _repo.UserTicket.RemoveTicketAndUser(usersticketFromDatabase); await _repo.Save(); //Getting the username and email from jwt token to set it to created by name and email var userName = User.Claims.ToList()[1].Value; var userEmail = User.Claims.ToList()[2].Value; ticketToUpdate.UpdatedByName = userName; ticketToUpdate.UpdatedByEmail = userEmail; //Assigning previous submitted by value because the submitter name will get deleted when we apply put method //And dont have to use this kind of trick for patch request but patch is little hard to implement var submmiteByName = originalTicket.SubmittedByName; var submittedByEmail = originalTicket.SubmittedByEmail; ticketToUpdate.SubmittedByName = submmiteByName; ticketToUpdate.SubmittedByEmail = submittedByEmail; ticketToUpdate.UpdatedAt = DateTime.Now; //now updating data _mapper.Map(ticketToUpdate, originalTicket); await _repo.Save(); return(Ok("Ticket Updated Sucessfully")); }
public async Task <IActionResult> UpdateTicket(int id, TicketForUpdateDto ticketForUpdateDto) { var ticketFromRepo = await _repo.GetTicket(id); _mapper.Map(ticketForUpdateDto, ticketFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new System.Exception($"Updating user {id} failed on save"); }
public void Update_Ticket() { var controller = new TicketsController(new TicketRepositoryMock(), new ClientRepositoryMock(), new InventoryServiceMock(), new HRServiceMock()); var ticketForUpdate = new TicketForUpdateDto() { Title = "What is up my dudez", Description = "This kenuWarew wont even turn on dudez", Status = "inProgress" }; var result = controller.Put(TicketRepositoryMock.TestTicket.Id, ticketForUpdate); var okResult = result.Should().BeOfType <NoContentResult>().Subject; }
public async Task <IActionResult> UpdateTicket(int id, TicketForUpdateDto ticketForUpdate) { var ticketFromRepo = await _repository.GetTicket(id); if (ticketFromRepo.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value) && UserRole.Admin.ToString() != User.FindFirst(ClaimTypes.Role).Value.ToString()) { return(Unauthorized()); } try { _mapper.Map(ticketForUpdate, ticketFromRepo); await _repository.SaveAll(); return(NoContent()); } catch (Exception) { return(NoContent()); } }
public IActionResult Put(int id, [FromBody] TicketForUpdateDto ticketForUpdate) { var ticket = _ticketRepository.Get(id); if (ticket == null) { return(NotFound("Could not find ticket")); } ticket.Title = ticketForUpdate.Title != null ? ticketForUpdate.Title : ticket.Title; ticket.Description = ticketForUpdate.Description != null ? ticketForUpdate.Description : ticket.Description; ticket.Status = ticketForUpdate.Status != null ? ticketForUpdate.Status : ticket.Status; _ticketRepository.Update(ticket); if (!_ticketRepository.Save()) { return(BadRequest("Could not update ticket")); } return(NoContent()); }