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(CreateProjectModel model) { if (_context.Projects.Any(p => p.Name == model.Title)) { throw new ServiceException($"Project with name '{model.Title}' already exists."); } if (string.IsNullOrEmpty(model.Title) || string.IsNullOrWhiteSpace(model.Title)) { throw new ServiceException("Title cannot be empty or just spaces."); } if (string.IsNullOrEmpty(model.Description) || string.IsNullOrWhiteSpace(model.Description)) { throw new ServiceException("Description cannot be empty or just spaces."); } if (model.Title.Length < 4) { throw new ServiceException("Title cannot have less than 4 characters."); } if (model.Description.Length < 6) { throw new ServiceException("Description cannot have less than 6 characters."); } var project = new DATA.Project { Name = model.Title, Description = model.Description, UserId = model.UserId }; _context.Add(project); _context.SaveChanges(); return(project.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); }
public static void SeedUsers(TicketingSystemDbContext context) { var user = new User() { Username = "******", Password = HashPassword("123"), FirstName = "test", LastName = "testenov", Email = "*****@*****.**" }; var user1 = new User() { Username = "******", Password = HashPassword("123"), FirstName = "test", LastName = "testenov", Email = "*****@*****.**" }; var user2 = new User() { Username = "******", Password = HashPassword("123"), FirstName = "test", LastName = "testenov", Email = "*****@*****.**" }; var user3 = new User() { Username = "******", Password = HashPassword("123"), FirstName = "test", LastName = "testenov", Email = "*****@*****.**" }; var user4 = new User() { Username = "******", Password = HashPassword("123"), FirstName = "test", LastName = "testenov", Email = "*****@*****.**" }; var admin = new User() { Username = "******", Password = HashPassword("123"), FirstName = "test", LastName = "testenov", Email = "*****@*****.**", Role = AccountRole.Administrator, AccountState = AccountState.Approved }; var support = new User() { Username = "******", Password = HashPassword("123"), FirstName = "Support", LastName = "Supportev", Email = "*****@*****.**", Role = AccountRole.Support, AccountState = AccountState.Approved }; context.AddRange(user, user1, user2, user3, user4, admin, support); context.SaveChanges(); }
public void Create(CreateUserModel model) { if (string.IsNullOrEmpty(model.FirstName)) { throw new ServiceException("Your first name cannot be empty."); } if (string.IsNullOrEmpty(model.LastName)) { throw new ServiceException("Your last name cannot be empty."); } if (string.IsNullOrEmpty(model.UserName)) { throw new ServiceException("Your username cannot be empty."); } if (_context.Users.Any(u => u.Username == model.UserName)) { throw new ServiceException("The username you have chosen already exists."); } if (string.IsNullOrEmpty(model.Email)) { throw new ServiceException("The email cannot be empty"); } var regex = new Regex(@"^([0-9a-zA-Z_]([_+-.\w]*[0-9a-zA-Z_])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"); Match match = regex.Match(model.Email); if (!match.Success) { throw new ServiceException("The email you enterted is in incorrect format"); } if (model.UserName.Length < 3) { throw new ServiceException("The username should be more than 2 characters"); } string password = HashPassword(model.Passowrd); DATA.User user = new DATA.User(); if (model.AccountState != AccountState.Pending) { user = new DATA.User { Username = model.UserName, Password = password, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; if (model.AccountState == AccountState.Approved) { user.AccountState = DATA.AccountState.Approved; } else { user.AccountState = DATA.AccountState.Denied; } _context.Add(user); _context.SaveChanges(); } else { user = new DATA.User { Username = model.UserName, Password = password, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, AccountState = DATA.AccountState.Pending }; _context.Add(user); _context.SaveChanges(); } }