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 int Create(CreateMessageModel model) { DATA.StateMessage messageState = (DATA.StateMessage)Enum.Parse(typeof(DATA.StateMessage), model.State); if (!Enum.IsDefined(typeof(DATA.StateMessage), messageState)) { throw new ServiceException("Invalid message state."); } var message = new DATA.Message { UserId = model.UserId, Content = model.Content, PublishingDate = model.PublishingDate, State = messageState, TicketId = model.TicketId }; _context.Add(message); if (!string.IsNullOrEmpty(model.FileName)) { DATA.File file = new DATA.File { Name = model.FileName, Content = model.FileContent, MessageId = message.Id }; _context.Files.Add(file); } _context.Add(message); _context.SaveChanges(); return(message.Id); }