//Creating a New User public void Post(UserDto userDto) { //Only Create a new user if they is not null, if they not already taken, and password is not null if (!string.IsNullOrWhiteSpace(userDto.Username) && !_context.Users.Any(x => x.Username == userDto.Username) && !string.IsNullOrWhiteSpace(userDto.Password)) { // including the DTO itens to user model var user = _mapper.Map <User>(userDto); //creating the hash password byte[] passwordSalt, passwordHash; CreatePasswordHash(userDto.Password, out passwordSalt, out passwordHash); user.PasswordSalt = passwordSalt; user.PasswordHash = passwordHash; _context.Users.Add(user); _context.SaveChanges(); } else { throw new ArgumentException("The fields name and password can't be null and the name need to be never used"); } }
//Creating a New Service public void Post(ServiceDto serviceDto) { //Only Create a new service if version and name is not null if (serviceDto.version > 0 && !string.IsNullOrWhiteSpace(serviceDto.name)) { // including the DTO itens to service model Service service = _mapper.Map <Service>(serviceDto); _context.Services.Add(service); _context.SaveChanges(); } else { throw new ArgumentException("The fields version and name can't be null."); } }
//Creating a New Toggle public void Post(SavingToggleDto toggleDto) { //Only Create a new toggle if Name and ServicesList is not null if (!string.IsNullOrWhiteSpace(toggleDto.Name) && toggleDto.ServicesList != null) { // including the DTO itens to toggle model //var toggle = _mapper.Map<Toggle>(toggleDto); Toggle toggle = new Toggle(); toggle.Name = toggleDto.Name; toggle.State = toggleDto.State; _context.Toggles.Add(toggle); _context.SaveChanges(); // including the Services having relatioship with this toggle if (toggleDto.ServicesList.First().ToString().Equals("All")) { foreach (var serviceItem in _context.Services.ToList()) { TogglesServices service = new TogglesServices(); service.ToggleId = toggle.Id; service.ServiceId = serviceItem.Id; _context.TogglesServices.Add(service); _context.SaveChanges(); } } else { foreach (var serviceItem in toggleDto.ServicesList) { TogglesServices service = new TogglesServices(); service.ToggleId = toggle.Id; service.ServiceId = Convert.ToInt32(serviceItem); _context.TogglesServices.Add(service); _context.SaveChanges(); } } } else { throw new ArgumentException("The fields name and ServicesList can't be null."); } }