应用DDD和API Rest

时间:2018-03-04 19:37:20

标签: rest api domain-driven-design

我正在开展一个关于考试的项目,这将使用Rest Api和DDD实现。在我的域名中,我有考试,问题和答案(实体)将考试(带问题和答案)视为单一聚合,因此考试将是聚合的根。我决定,因为至少有3个问题,考试的状态是活跃的,对于商业来说很重要。

class Exam extend AggregateRoot
{
    public create($title);…

    public makeQuestion($text, $answers, $rightAnswerId)
    {
        $question = Question::create($text, $answers, $rightAnswerId);
        $this->questions->add($question);

        if ($this->status !== ‘active’ and count($this->questions) => 3) {
            $this->status = ‘active’;
        } 
    }
}

class Question
{
    public create($text, $answers, $rightAnswerId)
    {
        $collection = [];

        foreach($answers as $answer) {
            $collection[] = Answer::create($answer[‘text’]); 
        } 

        return new self($text, $collection, $rightAnswerId);
    }
}


// first i’m gonna create exam
domain.com/api/v1/exam 
{
    title: ‘exam 1’
}

// second I’m gonna create question and answers together because of busines
domain.com/api/v1/exam/{id}/question

{
    text: ‘value?’,
    answers [
        {
            text: ‘value’
        },
        {
            text: ‘value2’
        },
    ],
    rightAnswerId: ‘1’
}

这个架构的问题是我不申请休息。因为我应该有以下端点:

domain.com/api/v1/exam //create exam
domain.com/api/v1/exam/{id}/question // create question
domain.com/api/v1/exam/{id}/question/{id}/answer // create answer

有人知道什么是正确的方法?

0 个答案:

没有答案