Ejemplo n.º 1
0
        public async Task <IActionResult> CreateAndNotifyReply([FromBody] CreateReplyCmd createNotifyCmd)
        {
            QuestionWriteContext ctx = new QuestionWriteContext(
                new EFList <Post>(_dbContext.Post),
                new EFList <User>(_dbContext.User));

            var dependencies = new QuestionDependencies();

            dependencies.GenerateConfirmationToken = () => Guid.NewGuid().ToString();
            dependencies.SendNotifyEmail           = SendNotifyEmail;

            var expr = from createReplyResult in QuestionDomain.CreateReply(createNotifyCmd)
                       let user = createReplyResult.SafeCast <CreateReplyResult.ReplyCreated>().Select(p => p.Author)
                                  let notifyReplyCmd = new NotifyReplyCmd(user)
                                                       from NotifyReplyResult in QuestionDomain.NotifyReply(notifyReplyCmd)
                                                       select new { createReplyResult };
            var r = await _interpreter.Interpret(expr, ctx, dependencies);

            _dbContext.Post.Add(new Post {
                PostTypeId = 2, PostText = createNotifyCmd.Body, PostedBy = new Guid("f505c32f-3573-4459-8112-af8276d3e919"), PostId = createNotifyCmd.QuestionId
            });
            await _dbContext.SaveChangesAsync();

            return(r.createReplyResult.Match(
                       created => (IActionResult)Ok(created.Answer.PostId),
                       notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Reply could not be created."),//todo return 500 (),
                       invalidRequest => BadRequest("Invalid request.")));
        }