Add Api package

MVVM
Emre KARTAL 1 year ago
parent 66f13688a5
commit 27bd45c071

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:Api">
</FileRef>
<FileRef
location = "group:AllInApp/AllInApp.xcodeproj">
</FileRef>

@ -10,6 +10,7 @@ import DependencyInjection
import Model
import ViewModel
import StubLib
import Api
@main
struct AllInApp: App {
@ -19,7 +20,7 @@ struct AllInApp: App {
init() {
DI.addSingleton(IAuthService.self, AuthService())
DI.addSingleton(ManagerVM.self, ManagerVM(withModel: Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserStubManager(username: "Imri"))))
DI.addSingleton(ManagerVM.self, ManagerVM(withModel: Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserApiManager())))
}
var body: some Scene {

@ -62,7 +62,7 @@ class AuthService: IAuthService {
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
print ("ALLIN : Process REGISTER")
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 200 {
if httpResponse.statusCode == 201 {
if let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let token = json["token"] as? String {

@ -32,7 +32,9 @@ class CreationBetViewModel: ObservableObject {
resetAllFieldErrors()
manager.addBet()
if let user = AppStateContainer.shared.user {
manager.addBet(theme: theme, description: description, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: user)
}
}
func checkAndSetError(forTheme checkTheme: Bool, forDescription checkDescription: Bool, forEndRegisterDate checkEndRegisterDate: Bool, forEndBetDate checkEndBetDate: Bool) -> Bool {

@ -97,23 +97,29 @@ struct CreationBetView: View {
.frame(width: 340)
.padding(.leading, 10)
TextField("", text: $viewModel.theme, prompt: Text("Études, sport, soirée...")
.foregroundColor(AllInColors.lightGrey300Color)
.font(.system(size: 14))
.fontWeight(.light))
.padding()
.background(
RoundedRectangle(cornerRadius: 9)
.fill(AllInColors.componentBackgroundColor)
.frame(height: 40)
)
.frame(width: 350, height: 40)
.foregroundColor(.black)
.overlay(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.stroke(AllInColors.delimiterGrey, lineWidth: 1)
)
.padding(.bottom, 5)
VStack {
if let themeError = $viewModel.themeFieldError.wrappedValue {
Text(themeError)
.textStyle(weight: .bold, color: .red, size: 10)
}
TextField("", text: $viewModel.theme, prompt: Text("Études, sport, soirée...")
.foregroundColor(AllInColors.lightGrey300Color)
.font(.system(size: 14))
.fontWeight(.light))
.padding()
.background(
RoundedRectangle(cornerRadius: 9)
.fill(AllInColors.componentBackgroundColor)
.frame(height: 40)
)
.frame(width: 350, height: 40)
.foregroundColor(.black)
.overlay(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.stroke(AllInColors.delimiterGrey, lineWidth: 1)
)
.padding(.bottom, 5)
}
}
HStack(spacing: 5) {
@ -132,24 +138,30 @@ struct CreationBetView: View {
.frame(width: 340)
.padding(.leading, 10)
TextField("", text: $viewModel.description, prompt: Text("David sera absent Lundi matin en cours ?")
.foregroundColor(AllInColors.lightGrey300Color)
.font(.system(size: 14))
.fontWeight(.light), axis: .vertical)
.lineLimit(4, reservesSpace: true)
.padding()
.background(
RoundedRectangle(cornerRadius: 9)
.fill(AllInColors.componentBackgroundColor)
.frame(height: 110)
)
.frame(width: 350, height: 110)
.foregroundColor(.black)
.overlay(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.stroke(AllInColors.delimiterGrey, lineWidth: 1)
)
.padding(.bottom, 30)
VStack {
if let descriptionError = $viewModel.descriptionFieldError.wrappedValue {
Text(descriptionError)
.textStyle(weight: .bold, color: .red, size: 10)
}
TextField("", text: $viewModel.description, prompt: Text("David sera absent Lundi matin en cours ?")
.foregroundColor(AllInColors.lightGrey300Color)
.font(.system(size: 14))
.fontWeight(.light), axis: .vertical)
.lineLimit(4, reservesSpace: true)
.padding()
.background(
RoundedRectangle(cornerRadius: 9)
.fill(AllInColors.componentBackgroundColor)
.frame(height: 110)
)
.frame(width: 350, height: 110)
.foregroundColor(.black)
.overlay(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.stroke(AllInColors.delimiterGrey, lineWidth: 1)
)
.padding(.bottom, 30)
}
HStack(spacing: 5) {
Text("Date de fin des inscriptions")
@ -166,19 +178,25 @@ struct CreationBetView: View {
.frame(width: 340)
.padding(.leading, 10)
HStack(spacing: 5) {
DatePicker(
"",
selection: $viewModel.endRegisterDate,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
.accentColor(AllInColors.lightPurpleColor)
.labelsHidden()
.padding(.bottom, 10)
Spacer()
VStack {
if let endRegisterError = $viewModel.endRegisterDateFieldError.wrappedValue {
Text(endRegisterError)
.textStyle(weight: .bold, color: .red, size: 10)
}
HStack(spacing: 5) {
DatePicker(
"",
selection: $viewModel.endRegisterDate,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
.accentColor(AllInColors.lightPurpleColor)
.labelsHidden()
.padding(.bottom, 10)
Spacer()
}
.frame(width: 340)
}
.frame(width: 340)
VStack(alignment: .leading, spacing: 5) {
VStack() {
@ -197,15 +215,25 @@ struct CreationBetView: View {
}
.padding(.leading, 10)
}
DatePicker(
"",
selection: $viewModel.endBetDate,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
.accentColor(AllInColors.lightPurpleColor)
.labelsHidden()
.padding(.bottom, 40)
VStack {
if let endBetError = $viewModel.endBetDateFieldError.wrappedValue {
Text(endBetError)
.textStyle(weight: .bold, color: .red, size: 10)
}
HStack(spacing: 5) {
DatePicker(
"",
selection: $viewModel.endBetDate,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
.accentColor(AllInColors.lightPurpleColor)
.labelsHidden()
.padding(.bottom, 40)
Spacer()
}
}
}
.frame(width: 340)

@ -60,6 +60,7 @@
ECB7BC6A2B2F410A002A6654 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB7BC692B2F410A002A6654 /* AppDelegate.swift */; };
ECB7BC6C2B2F43EE002A6654 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB7BC6B2B2F43EE002A6654 /* AppState.swift */; };
ECB7BC702B336E28002A6654 /* RegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB7BC6F2B336E28002A6654 /* RegisterViewModel.swift */; };
ECCD244A2B4DE8010071FA9E /* Api in Frameworks */ = {isa = PBXBuildFile; productRef = ECCD24492B4DE8010071FA9E /* Api */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -156,6 +157,7 @@
files = (
EC8B9CCE2B42C9D3002806F3 /* StubLib in Frameworks */,
ECB357352B3E13A400045D41 /* Model in Frameworks */,
ECCD244A2B4DE8010071FA9E /* Api in Frameworks */,
ECB357312B3CA69300045D41 /* DependencyInjection.framework in Frameworks */,
ECB26A152B406B4800FE06B3 /* ViewModel in Frameworks */,
);
@ -354,6 +356,7 @@
ECB357342B3E13A400045D41 /* Model */,
ECB26A142B406B4800FE06B3 /* ViewModel */,
EC8B9CCD2B42C9D3002806F3 /* StubLib */,
ECCD24492B4DE8010071FA9E /* Api */,
);
productName = AllIn;
productReference = EC6B96982B24B4CC00FC1C58 /* AllIn.app */;
@ -852,6 +855,10 @@
isa = XCSwiftPackageProductDependency;
productName = Model;
};
ECCD24492B4DE8010071FA9E /* Api */ = {
isa = XCSwiftPackageProductDependency;
productName = Api;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = EC6B96902B24B4CC00FC1C58 /* Project object */;

@ -0,0 +1,25 @@
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Api",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Api",
targets: ["Api"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Api",
dependencies: []),
]
)

@ -0,0 +1,3 @@
# Api
A description of this package.

@ -0,0 +1,52 @@
//
// UserApiManager.swift
//
//
// Created by Emre on 31/12/2023.
//
import Foundation
import Model
let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api"
public struct UserApiManager: UserDataManager {
public init() {
}
public func getBets(withIndex index: Int, withCount count: Int) -> [Bet] {
fatalError("Not implemented yet")
}
public func addBet(bet: Bet) {
let url = URL(string: allInApi + "bets/add")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let json = [
"theme": bet.theme,
"sentenceBet": bet.phrase,
"endRegistration": dateFormatter.string(from: bet.endRegisterDate),
"endBet": dateFormatter.string(from: bet.endBetDate),
"isPrivate": String(bet.isPublic),
"response": "",
"createdBy": ""
]
if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
}.resume()
}
}
public func getFriends() -> [User] {
fatalError("Not implemented yet")
}
}

@ -21,8 +21,5 @@ let package = Package(
.target(
name: "Model",
dependencies: []),
.testTarget(
name: "ModelTests",
dependencies: ["Model"]),
]
)

@ -0,0 +1,14 @@
//
// FactoryBet.swift
//
//
// Created by Emre on 09/01/2024.
//
import Foundation
public protocol FactoryBet {
func toResponse()
func toModel() -> Bet
func toModel(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet
}

@ -16,4 +16,8 @@ public struct Manager {
self.userDataManager = userDataManager
}
public func addBet(bet: Bet) {
userDataManager.addBet(bet: bet)
}
}

@ -22,8 +22,5 @@ let package = Package(
.target(
name: "StubLib",
dependencies: ["Model"]),
.testTarget(
name: "StubLibTests",
dependencies: ["StubLib"]),
]
)

@ -27,7 +27,7 @@ struct Stub {
users.append(user2)
user1.addFriend(user: user2)
var user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30, friends: [user2])
let user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30, friends: [user2])
users.append(user3)
user2.addFriend(user: user3)

@ -25,8 +25,5 @@ let package = Package(
.target(
name: "ViewModel",
dependencies: ["Model"]),
.testTarget(
name: "ViewModelTests",
dependencies: ["ViewModel"]),
]
)

@ -19,7 +19,7 @@ public class ManagerVM: ObservableObject {
}
public func addBet() {
public func addBet(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User) {
model.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: []))
}
}

Loading…
Cancel
Save