Untitled

mail@pastecode.io avatar
unknown
swift
2 years ago
3.6 kB
4
Indexable
Never
import Foundation
import FoundationNetworking



struct Groups {
	var name: String?
  var lists: [Lists]?



struct Lists {
	var name: String?
	var taskList: [String]?
}


	struct Tasks {
	var description: String?
	var status: Bool
}


}


struct Users {
	//var name: String?
	var email: String?
	var password: String?
	var groups: [Groups]?
}

struct User: Codable {
	//var name: String?
	var email: String?
	var password: String?
	//var groups: [Groups]?
}


// var group1 = Groups(name: "grupa1", Lists.init(name: "", taskList: []), 
//  	Tasks.init(description: "", status: false))
 //group1.init(name: "grupa1", Groups.Lists.init(name: "", taskList: []), 
 	//Groups.Tasks.init(description: "", status: false))


var groups: [Groups] = []

var group1 = Groups(name: "grupa1", lists: [])
var group2 = Groups(name: "grupa2", lists: [])

//groups.append(group1)
//groups.append(group2)


//print(groups.first(where: {$0.name == "grupa1"}))

var user1 = Users(email: "oko@wp.pl",groups: [])
var users: [Users] = []

users.append(user1)

print(users.first(where: {$0.email == "oko@wp.pl"}))

var list1 = Groups.Lists.init(name: "list1", taskList: [])
var listy: [Groups.Lists] = []
//lists.append(Groups.Lists.init(name: "List1", lists: []))

//print(lists)
//group1.lists = lists
//users.first(where: {$0.email == "oko@wp.pl"}).groups.append(group1)
//users.map({$0.email == "oko@wp.pl" })
print(users.firstIndex(where: {$0.email == "oko@wp.pl" })!)
var index = users.firstIndex(where: {$0.email == "oko@wp.pl" })!
print(users.map({$0.email == "oko@wp.pl" }))
//groups.append(group1)
print(users[index])
users[index].groups?.append(group1)
print(users[index])

var index_group = users[index].groups?.firstIndex(where: {$0.name == "grupa1"})!
users[index].groups![index_group!].lists?.append(list1)
print(users[index])

var task1 = Groups.Tasks.init(description: "task1", status: false)

var index_list = users[index].groups![index_group!].lists?.firstIndex(where: {$0.name == "list1"})!
users[index].groups![index_group!].lists![index_list!].taskList?.append("task1")
print(users[index])


enum HTTp {
  enum Method: String {
    case get = "GET"
    case post = "POST"
    case put = "PUT"
  }


}

//func checkUser() {
let todoEndpoint: String = "https://weaweg.mywire.org:8080/api/users/register"

let userCall = 
[  
  "email": "bb@gmail.com",
  "password": "bb"
]

let bodyData = try? JSONSerialization.data(
  withJSONObject: userCall
)


guard let url = URL(string: todoEndpoint) else {
    print("Error: cannot create URL")
    exit(1)
}

let session = URLSession.shared
var request = URLRequest(url: url)

request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-type")
request.httpBody = bodyData


do {
  request.httpBody = try JSONSerialization.data(withJSONObject: userCall, options: .prettyPrinted)
} catch let error {
  print(error.localizedDescription)
  //return
}

let task = session.dataTask(with: request) { (data, response, error) in 

if let httpResponse = response as? HTTPURLResponse {
  print("statusCode: \(httpResponse.statusCode)")
} else {
  print("yo")
}

if error == nil && data != nil {
  let decoder = JSONDecoder()
  do {

let model_list = try decoder.decode(User.self, from: bodyData!)
print(model_list)


} catch {
  print("Error in json parsing")
}
print(response)

}
}
task.resume()


//checkUser()