Skip to content

Commit

Permalink
adding user table
Browse files Browse the repository at this point in the history
fix scheme
  • Loading branch information
charlesweng committed Nov 14, 2024
1 parent 42fbe04 commit 04adb05
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
marchingCubes/marchingCubes.xcodeproj/project.xcworkspace/xcuserdata/charlesweng.xcuserdatad/UserInterfaceState.xcuserstate
marchingCubes/marchingCubes.xcodeproj/project.xcworkspace/xcuserdata/charlesweng.xcuserdatad/UserInterfaceState.xcuserstate
marchingCubes/marchingCubes.xcodeproj/project.xcworkspace/xcuserdata/charlesweng.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
34 changes: 34 additions & 0 deletions marchingCubes/marchingCubes/Api/ProfilePictureApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ func generateRandomString(length: Int = 5) -> String {
return String((0..<length).compactMap { _ in letters.randomElement() })
}

func fetchSVGBase64(from urlString: String = "https://api.dicebear.com/9.x/lorelei/svg?seed=\(generateRandomString())", completion: @escaping (String?) -> Void) {
guard let url = URL(string: urlString) else {
completion(nil)
return
}

let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
completion(nil)
return
}

// Convert SVG data to Base64-encoded string
let base64String = data.base64EncodedString()
let svgBase64String = "data:image/svg+xml;base64,\(base64String)"

completion(svgBase64String)
}

task.resume()
}

func loadSVGImage(from base64String: String) -> UIImage? {
// Remove the "data:image/svg+xml;base64," prefix if present
guard let data = Data(base64Encoded: base64String.replacingOccurrences(of: "data:image/svg+xml;base64,", with: "")) else {
print("Failed to decode Base64 string.")
return nil
}

// Initialize SVGKImage from Data
let svgImage = SVGKImage(data: data)
return svgImage?.uiImage
}

func fetchSVGImage(from urlString: String = "https://api.dicebear.com/9.x/lorelei/svg?seed=\(generateRandomString())", completion: @escaping (UIImage?) -> Void) {
guard let url = URL(string: urlString) else {
completion(nil)
Expand Down
2 changes: 1 addition & 1 deletion marchingCubes/marchingCubes/Auth/CognitoAuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CognitoAuthManager {

func signUp(username: String, password: String, email: String, completion: @escaping (Result<Void, Error>) -> Void) async {
let signUpInput = SignUpInput(
clientId: "68g2sfl6kbeacqrekd8s8oq2u2",
clientId: "793iigcjhdaj0309vq8q00v9ps",
password: password,
userAttributes: [
CognitoIdentityProviderClientTypes.AttributeType.init(name: "email", value: email)
Expand Down
70 changes: 55 additions & 15 deletions marchingCubes/marchingCubes/Models/DynamoDBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,72 @@
//

import Foundation
import AWSClientRuntime
import AWSDynamoDB

class DynamoDBManager {

let dynamoDB: AWSDynamoDB.DynamoDBClient

init() throws {
self.dynamoDB = try AWSDynamoDB.DynamoDBClient(region: "us-east-2")
init() async throws {
// let credentialsProvider = AWSClientRuntime.(accessKey: ProcessInfo.processInfo.environment["AWS_ACCESS_KEY_ID"],
// secretKey: ProcessInfo.processInfo.environment["AWS_SECRET_ACCESS_KEY"])
do {
let config = try await DynamoDBClient.DynamoDBClientConfiguration(region: "us-east-2")
self.dynamoDB = AWSDynamoDB.DynamoDBClient(config: config)
} catch {
print("Error: ", dump(error, name: "Initializing Amazon DynamoDBClient client"))
throw error
}
}

func createTable() async throws {
do {
let client = self.dynamoDB

let input = CreateTableInput(
attributeDefinitions: [
DynamoDBClientTypes.AttributeDefinition(attributeName: "year", attributeType: .n),
DynamoDBClientTypes.AttributeDefinition(attributeName: "title", attributeType: .s)
],
keySchema: [
DynamoDBClientTypes.KeySchemaElement(attributeName: "year", keyType: .hash),
DynamoDBClientTypes.KeySchemaElement(attributeName: "title", keyType: .range)
],
provisionedThroughput: DynamoDBClientTypes.ProvisionedThroughput(
readCapacityUnits: 10,
writeCapacityUnits: 10
),
tableName: "movies"
)
let output = try await client.createTable(input: input)
if output.tableDescription == nil {
print("error: in tble")
return
}
} catch {
print("ERROR: createTable:", dump(error))
throw error
}
}


func getUserAsItem(userModel: UserModel) async throws -> [Swift.String:DynamoDBClientTypes.AttributeValue] {
// Convert each project and favorite to a DynamoDB AttributeValue
let projectsAttributeValue = userModel.projects.map { DynamoDBClientTypes.AttributeValue.s($0) }
let favoritesAttributeValue = userModel.favorites.map { DynamoDBClientTypes.AttributeValue.s($0) }
// Convert each project and favorite to a DynamoDB AttributeValue
let projectsAttributeValue = userModel.projects.map { DynamoDBClientTypes.AttributeValue.s($0) }
let favoritesAttributeValue = userModel.favorites.map { DynamoDBClientTypes.AttributeValue.s($0) }

let item: [Swift.String:DynamoDBClientTypes.AttributeValue] = [
"id": .s(userModel.id),
"email": .s(userModel.email),
"profile_image": .s(userModel.profile_image),
"projects": .l(projectsAttributeValue),
"favorites": .l(favoritesAttributeValue),
"created_timestamp": .n(String(userModel.created_timestamp))
]
let item: [Swift.String:DynamoDBClientTypes.AttributeValue] = [
"id": .s(userModel.id),
"email": .s(userModel.email),
"profile_image": .s(userModel.profile_image),
"projects": .l(projectsAttributeValue),
"favorites": .l(favoritesAttributeValue),
"created_timestamp": .n(String(userModel.created_timestamp))
]

return item
}
return item
}

func insertUserModel(userModel: UserModel) async throws {
let client = self.dynamoDB
Expand Down
15 changes: 13 additions & 2 deletions marchingCubes/marchingCubes/Views/AuthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,24 @@ struct SignUpView: View {
case .failure(let error):
print("Sign-up failed with error: \(error)")
handleSignUpError(error)
// Handle sign-up failure logic here
}
}
if (isAuthenticated) {
var data: String = ""
fetchSVGBase64 {image in
DispatchQueue.main.async {
data = image!
}
}
try await
DynamoDBManager().createTable()
// try await DynamoDBManager().insertUserModel(userModel: UserModel(
// id: username, email: email, profile_image: data, projects: [], favorites: [], created_timestamp: Int(Date().timeIntervalSince1970)
// ))
}
} catch {
print("Unexpected error: \(error)")
handleSignUpError(error)
// Handle unexpected error logic here
}
}

Expand Down
3 changes: 0 additions & 3 deletions marchingCubes/marchingCubes/Views/Dashboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
// Dashboard()
// }
//}

import SwiftUI
import SwiftData

Expand Down Expand Up @@ -248,8 +247,6 @@ struct Dashboard: View {
}
}
}


}

struct Dashboard_Previews: PreviewProvider {
Expand Down
5 changes: 2 additions & 3 deletions marchingCubes/marchingCubes/Views/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Created by 温嘉宝 on 22.10.2024.
//

import SwiftUI

struct ProfileView: View {
Expand Down Expand Up @@ -50,9 +49,9 @@ struct ProfileView: View {
}
.padding()
}.onAppear {
fetchSVGImage { image in
fetchSVGBase64 { image in
DispatchQueue.main.async {
self.avatarImage = image
self.avatarImage = loadSVGImage(from: image ?? "")
}
}
}
Expand Down

0 comments on commit 04adb05

Please sign in to comment.