public void Edit(UpdateTicketModel model) { DATA.TicketType ticketType = (DATA.TicketType)Enum.Parse(typeof(DATA.TicketType), model.Type); if (!Enum.IsDefined(typeof(DATA.TicketType), ticketType)) { throw new ServiceException("Invalid Ticket Type."); } DATA.TicketState ticketState = (DATA.TicketState)Enum.Parse(typeof(DATA.TicketState), model.State); if (!Enum.IsDefined(typeof(DATA.TicketState), ticketState)) { throw new ServiceException("Invalid Ticket State."); } if (string.IsNullOrEmpty(model.Title) || model.Title.Length < 5 || string.IsNullOrWhiteSpace(model.Title)) { throw new ServiceException("The Ticket title should have no less than 5 characters."); } if (string.IsNullOrEmpty(model.Description) || model.Description.Length < 5) { throw new ServiceException("The description should have no less than 5 characters."); } DATA.Ticket ticket = _context.Tickets.First(t => t.Id == model.Id); ticket.Description = model.Description; ticket.Title = model.Title; ticket.Type = ticketType; ticket.State = ticketState; _context.SaveChanges(); }
public int Create(CreateTicketModel model) { DATA.TicketType ticketType = (DATA.TicketType)Enum.Parse(typeof(DATA.TicketType), model.TicketType); if (!Enum.IsDefined(typeof(DATA.TicketType), ticketType)) { throw new ServiceException("Invalid Ticket Type."); } DATA.TicketState ticketState = (DATA.TicketState)Enum.Parse(typeof(DATA.TicketState), model.TicketState); if (!Enum.IsDefined(typeof(DATA.TicketState), ticketState)) { throw new ServiceException("Invalid Ticket State."); } if (string.IsNullOrEmpty(model.TicketTitle) || model.TicketTitle.Length < 5 || string.IsNullOrWhiteSpace(model.TicketTitle)) { throw new ServiceException("The Ticket title should have no less than 5 characters."); } if (string.IsNullOrEmpty(model.TicketDescription) || model.TicketDescription.Length < 5) { throw new ServiceException("The description should have no less than 5 characters."); } if (_context.Tickets.Any(a => a.ProjectId == model.ProjectId && a.Title == model.TicketTitle)) { throw new ServiceException($"This project already has ticket with title '{model.TicketTitle}'."); } DATA.Ticket ticket = new DATA.Ticket() { ProjectId = model.ProjectId, Description = model.TicketDescription, SubmissionDate = DateTime.Now, Title = model.TicketTitle, Type = ticketType, State = ticketState, SubmitterId = model.SubmitterId, }; _context.Tickets.Add(ticket); if (!string.IsNullOrEmpty(model.FileName)) { DATA.File file = new DATA.File { Name = model.FileName, Content = model.FileContent, TicketId = ticket.Id, }; _context.Files.Add(file); } _context.SaveChanges(); return(ticket.Id); }
public void ChangeType(UpdateTicketModel model) { DATA.Ticket ticket = _context.Tickets.First(t => t.Id == model.Id); if (ticket == null) { throw new ServiceException("Ticket not found."); } if (!string.IsNullOrEmpty(model.Type)) { DATA.TicketType type = (DATA.TicketType)Enum.Parse(typeof(DATA.TicketType), model.Type); ticket.Type = type; } _context.SaveChanges(); }