public async Task <IActionResult> Register(Login login) { if (string.IsNullOrEmpty(login.Username)) { return(BadRequest(new { error = true, message = "Username must not be empty" })); } if (string.IsNullOrEmpty(login.Email)) { return(BadRequest(new { error = true, message = "Email must not be empty" })); } if (string.IsNullOrEmpty(login.Password)) { return(BadRequest(new { error = true, message = "Password must not be empty" })); } var usernames = await ctx.Logins.Select(l => l.Username).ToListAsync(); var emails = await ctx.Logins.Select(l => l.Email).ToListAsync(); if (usernames.Contains(login.Username)) { return(BadRequest(new { error = true, message = "Username already taken" })); } if (emails.Contains(login.Email)) { return(BadRequest(new { error = true, message = "Email already taken" })); } login.Password = BCrypt.Net.BCrypt.HashPassword(login.Password); ctx.Add(login); await ctx.SaveChangesAsync(); AuthenticateRequest model = new AuthenticateRequest { Username = login.Username, Password = login.Password }; var response = await _userService.Authenticate(model); if (response == null) { return(BadRequest(new { message = "Account registered but could not log in automatically", error = true })); } return(Ok(new { username = response.Username, email = response.Email, token = response.Token, error = false, message = "Successfully registered" })); }
public async Task <IActionResult> SaveConnection(SaveConnectionRequest request) { // int connectionId = request.ConnectionId; DatabaseTable[] tablesGivenToSaveToDatabase = request.Tables; //get connections to which to save tables var connection = await ctx.ConnectionTables.Where(connection => connection.ConnectionId == connectionId).FirstOrDefaultAsync(); var tablesStoredInApplicationDatabaseForThisConnection = await ctx.DatabaseTables.Where(t => t.ConnectionId == connectionId).Include(t => t.DatabaseColumns).ToListAsync(); foreach (DatabaseTable tableGivenToSaveToDatabase in tablesGivenToSaveToDatabase) { //get corresponding table from own database DatabaseTable tableStoredInApplicationDatabase = tablesStoredInApplicationDatabaseForThisConnection.Find(t => t == tableGivenToSaveToDatabase); //if no corresponding table is stored in application database for given connection create new table if (tableStoredInApplicationDatabase == null) { DatabaseTable newTable = new DatabaseTable { Name = tableGivenToSaveToDatabase.Name, Connection = connection, ConnectionId = connectionId, DatabaseColumns = tableGivenToSaveToDatabase.DatabaseColumns, NumberOfColumnsToGenerate = tableGivenToSaveToDatabase.NumberOfColumnsToGenerate }; try { await ctx.AddAsync(newTable); } catch (Exception e) { return(BadRequest(new { error = true, message = e.Message })); } } else { tableStoredInApplicationDatabase.CopyRelevantGenerationValues(tableGivenToSaveToDatabase); var columnsStoredInApplicationDatabase = tableStoredInApplicationDatabase.DatabaseColumns; var columnsGivenToSaveToDatabase = tableGivenToSaveToDatabase.DatabaseColumns; // For every column given to save, check if there is corresponding table, if there is none create new one, if not modify existing column to fit new one foreach (var columnGivenToSaveToDatabase in columnsGivenToSaveToDatabase) { //if there is no column add it var columnStoredInApplicationDatabase = columnsStoredInApplicationDatabase.Where(c => c == columnGivenToSaveToDatabase).FirstOrDefault(); // If there is no corresponding column for column that needs to be saved to database add the new column to the database if (columnStoredInApplicationDatabase == null) { columnsStoredInApplicationDatabase.Add(columnGivenToSaveToDatabase); continue; } // If corresponding table exists modify existing one to match new one columnStoredInApplicationDatabase.CopyRelevantGenerationValues(columnGivenToSaveToDatabase); } } } await ctx.SaveChangesAsync(); return(Ok(new { error = false, message = "Successfully saved" })); }