Search code examples
swiftvapor

How can I use the parent id to put the children's data?


   final class InformationController{

    func create (_ req: Request) throws -> EventLoopFuture<Pers>{
        let perinfo=try req.content.decode(Pers.self)
        return perinfo.save(on: req.db).map {perinfo}
    }

    func putpers (_ req: Request) throws -> EventLoopFuture<HTTPStatus>  {
            let information = try req.content.decode(Pers.self)
            return Pers.find(information.$user.id, on: req.db)
                .unwrap(or: Abort(.notFound))
                .flatMap{
                    $0.name=information.name
                    $0.account=information.account
                    $0.picture=information.picture
                    $0.age=information.age
                    $0.birth=information.birth
                    $0.city=information.city
                    $0.hobby=information.hobby
                    return $0.update(on: req.db).transform(to: .ok)
                }
        }
}
final class UserController{

    func signup (_ req: Request) throws -> EventLoopFuture<User>{
            
            try User.Create.validate(content: req)
    
                // 從request body中decode出User.Create
            let newUser = try req.content.decode(User.Create.self)
                // 先檢查密碼是否==確認密碼
            guard newUser.password == newUser.confirmPassword else {
                throw Abort(.badRequest, reason: "Passwords did not match")
            }
                // 將User.Create 轉成 User
            let user = try User(account: newUser.account, password: Bcrypt.hash(newUser.password))
                // 儲存User並回傳
            return user.save(on: req.db).map { user }
        }
}

This is my controller. I use the User as a parent. And Pers as a children. But when I use postman to do the api. It kept telling me this:

enter image description here Cause I thought that using 2 ids to do the action is a little bit weird. I make the relation as one to many. Because I can't find Child in scope

enter image description here

routes:

    import Fluent
import Vapor

func routes(_ app: Application) throws {
    
    let userController = UserController()
    let informationController=InformationController()
    let passwordProtected = app.grouped(User.authenticator(),User.guardMiddleware())
    let tokenProtected = app.grouped(UserToken.authenticator(),UserToken.guardMiddleware())
    

    
    
    //修改密碼
    app.put("user",":userId", use:userController.update)//
    
    //第一次上傳個人資料
    app.post("information", use:informationController.create)//
    
    //更新個人資料
    app.put("information", use:informationController.new)
    
    //註冊
    app.post("signup", use:userController.signup)//ok
    
    //登陸
    passwordProtected.post("login",use: userController.login)//
    
    //登陸後收到個人資料
    tokenProtected.get("getperinfo",use: userController.getperinfo)//
    
    //收到他人資料
    app.get("otherperinfo",":userId",use:informationController.otherperinfo)//
    
    //登出
    app.delete("logout",":user_tokensId", use: userController.logout)//
}

parent model:

import Foundation
import Fluent
import Vapor
import FluentPostgresDriver


final class User:Model,Content{
    static let schema = "user"
    
    @ID(key: .id)
    var id:UUID?
    
    @Field(key:"帳號")
    var account:String
    
    
    @Field(key: "密碼")
    var password:String
    
    @Children(for: \.$user)
    var perinfo: [Pers]
    
    
    init() {}
    
    init(id: UUID?=nil, account:String, password:String){
        self.id=id
        self.account=account
        self.password=password
    }    
}

children model:

 import Foundation
import Fluent
import Vapor
import FluentPostgresDriver




    final class Pers:Model,Content{
        static let schema = "pers"
        
        @ID(key: .id)
        var id:UUID?
        
        @Field(key: "姓名")
        var name: String
        
        @Field(key: "IG帳號")
        var account: String
        
        @Field(key: "頭像")
        var picture: String
        
        @Field(key: "年紀")
        var age: String
        
        @Field(key: "生日")
        var birth: String
        
        @Field(key: "居住城市")
        var city: String
        
        @Field(key: "興趣")
        var hobby : String
        
        @Parent(key: "user_id")
        var user: User
        
        
        
        init(){}
        
        init(id:UUID?=nil, name:String, account:String, picture:String ,age:String, birth:String, city:String, hobby:String, userId:UUID){
            self.id=id
            self.name=name
            self.account=account
            self.picture=picture
            self.age=age
            self.birth=birth
            self.city=city
            self.hobby=hobby
            self.$user.id=userId
        }
    }

Solution

  • The issue you have is that you're sending the API a flat JSON object, bet trying to decode it to a model with a parent-child relationship. Fluent's property wrappers make it easy to show relationships but it works the other way as well.

    You have two options:

    Change the JSON you send to match what Vapor expects, so it would look something like:

    {
      "name": "dog",
      "account": "222", 
      // ...
      "user": {
        "id": "<USER_ID>"
      }
    }
    

    Or you can create a DTO that matches the data you expect the client to send to you, decode that, convert it in to a Pers and save that. See the Vapor TIL repo for an example.