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 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 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(); } }