public async Task <IActionResult> CreateQuestion([FromBody] CreateQuestionCmd cmd) { QuestionsWriteContext ctx = new QuestionsWriteContext( new EFList <Post>(_dbContext.Post)); var dep = new QuestionsDependencies(); var questions = await _dbContext.Post.ToListAsync(); //var questions = await _dbContext.Post.Where(q => q.PostTypeId == 1).ToListAsync(); _dbContext.Post.AttachRange(questions); var expr = from createQuestionResult in QuestionsDomain.CreateQuestion(cmd) //let checkLanguageCmd = new CheckLanguageCmd() //select CreateQuestionResult; from checkLanguageResult in QuestionsDomain.CheckLanguage(new CheckLanguageCmd(cmd.PostText)) //from sendAckToQuestionOwnerCmd in QuestionsDomain.SendQuestionOwnerAcknowledgement(new SendQuestionOwnerAcknowledgementCmd()) select new { createQuestionResult, checkLanguageResult }; var r = await _interpreter.Interpret(expr, ctx, dep); //var reply = await _dbContext.QuestionModel.Where(r => r.Title == "intrebTest").SingleOrDefaultAsync(); //_dbContext.Question.Update(reply); //await _dbContext.SaveChangesAsync(); _dbContext.SaveChanges(); return(r.createQuestionResult.Match( created => (IActionResult)Ok("PostID of question created:" + created.question.PostId), notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Question could not be created."),//todo return 500 (), invalidRequest => BadRequest("Invalid request."))); }
public async Task <IActionResult> CreateAnswer([FromBody] CreateAnswerCmd cmd) { QuestionsWriteContext ctx = new QuestionsWriteContext( new EFList <Post>(_dbContext.Post)); var dep = new QuestionsDependencies(); //dep.GenerateConfirmationToken = () => Guid.NewGuid().ToString(); //dep.SendConfirmationEmail = SendConfirmationEmail var answers = await _dbContext.Post.ToListAsync(); _dbContext.Post.AttachRange(answers); var expr = from createAnswerResult in QuestionsDomain.CreateAnswer(cmd) //let checkLanguageCmd = new CheckLanguageCmd() from checkLanguageResult in QuestionsDomain.CheckLanguage(new CheckLanguageCmd(cmd.PostText)) //from sendAckAuthor in QuestionsDomain.SendAnswerAuthorAcknowledgement(new SendAnswerAuthorAcknowledgementCmd(Guid.NewGuid(), 1, 2)) select new { createAnswerResult, checkLanguageResult }; var r = await _interpreter.Interpret(expr, ctx, dep); //_dbContext.Replies.Add(new DatabaseModel.Models.Reply { Body = cmd.PostText, AuthorUserId = 1, QuestionId = cmd.QuestionId, ReplyId = 4 }); // var reply = await _dbContext.Replies.Where(r => r.ReplyId == 4).SingleOrDefaultAsync(); //reply.Body = "Text updated"; //_dbContext.Replies.Update(reply); //await _dbContext.SaveChangesAsync(); _dbContext.SaveChanges(); return(r.createAnswerResult.Match( created => (IActionResult)Ok("PostID of answer created: " + created.answer.PostId), notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Answer could not be created."),//todo return 500 (), invalidRequest => BadRequest("Invalid request."))); }
public async Task <IActionResult> CreateReply([FromBody] int questionId) { //database var posts = _dbContext.Post.ToList(); var questionWriteContext = new QuestionWriteContext(new EFList <Post>(_dbContext.Post)); /*var questionWriteContext = new QuestionWriteContext(new List<Post>() //ctx * { * new Post() * { * PostId=10, * PostText="Intrebare?" * } * });*/ //var questionDependencies = new QuestionDependencies();//dependencies //questionDependencies.GenerateConfirmationEmail = () => Guid.NewGuid().ToString(); //questionDependencies.SentEmail = (LogInForQuestions login) => async () => new ConfirmationEmail(Guid.NewGuid().ToString()); var expr = from replyResult in QuestionsDomain.CreateReply(questionId, "8989") select replyResult; var result = await _interpreter.Interpret(expr, questionWriteContext /*Unit.Default*/, new object()); //CreateReplyResult.ICreateReplyResult result = await _interpreter.Interpret(expr, questionWriteContext, questionDependencies); await _dbContext.SaveChangesAsync(); return(result.Match(created => (IActionResult)Ok(created), notCreated => BadRequest(notCreated), invalidRequest => ValidationProblem() )); }
public async Task <IActionResult> CreateReplyAsync(int questionId, [FromBody] CreateAnswerCmdInternal createAnswerCmd) { var ctx = new QuestionsWriteContext(new EFList <Post>(_dbContext.Post), new EFList <User>(_dbContext.User)); var dependencies = new QuestionsDependency(); dependencies.GenerateConfirmationToken = () => Guid.NewGuid().ToString(); dependencies.SendConfirmationEmail = SendEmailToOwner; var expr = from createResult in QuestionsDomain.CreateAnswer(createAnswerCmd.TenantId, createAnswerCmd.PostId, createAnswerCmd.UserId, questionId, createAnswerCmd.Answer, createAnswerCmd.User, createAnswerCmd.Title) let user = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.User) let tenantId = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.TenantId) let userId = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.UserId) let QuestionId = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.QuestionId) let answer = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.Answer) let isPublished = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.IsPublished) let postId = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.PostId) let title = createResult.SafeCast <CreateAnswerResult.ReplyPublished>().Select(p => p.Title) let confirmReply = new CreateAnswerCmdInternal((int)tenantId, (int)postId, (Guid)userId, (int)QuestionId, (string)answer, (User)user, (string)title) from CreateAnswerResult in QuestionsDomain.CreateAnswer(((CreateAnswerCmdInternal)confirmReply).TenantId, ((CreateAnswerCmdInternal)confirmReply).PostId, ((CreateAnswerCmdInternal)confirmReply).UserId, ((CreateAnswerCmdInternal)confirmReply).QuestionId, ((CreateAnswerCmdInternal)confirmReply).Answer, ((CreateAnswerCmdInternal)confirmReply).User, ((CreateAnswerCmdInternal)confirmReply).Title) select new { createResult, CreateAnswerResult }; var r = await _interpreter.Interpret(expr, ctx, dependencies); _dbContext.SaveChanges(); return(r.createResult.Match( created => (IActionResult)Ok(created.Answer), notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Question could not be created."), invalidRequest => BadRequest("Invalid request."))); }
public async Task <IActionResult> CreateQuestionAsync([FromBody] CreateQuestionCmd createQuestionCmd) { var ctx = new QuestionsWriteContext(new EFList <Post>(_dbContext.Post), new EFList <User>(_dbContext.User)); var dependencies = new QuestionsDependency(); dependencies.GenerateConfirmationToken = () => Guid.NewGuid().ToString(); dependencies.SendConfirmationEmail = SendEmailToOwner; var expr = from createResult in QuestionsDomain.CreateQuestion(createQuestionCmd) let title = createResult.SafeCast <CreateQuestionResult.QuestPublished>().Select(p => p.Title) let body = createResult.SafeCast <CreateQuestionResult.QuestPublished>().Select(p => p.Body) let tag = createResult.SafeCast <CreateQuestionResult.QuestPublished>().Select(p => p.Tag) let tenantId = createResult.SafeCast <CreateQuestionResult.QuestPublished>().Select(p => p.TenantId) let userId = createResult.SafeCast <CreateQuestionResult.QuestPublished>().Select(p => p.UserId) let user = createResult.SafeCast <CreateQuestionResult.QuestPublished>().Select(p => p.User) let confirmQuest = new CreateQuestionCmd((string)title, (string)body, (string)tag, (int)tenantId, (Guid)userId, (TenantUser)user) from CreateQuestionResult in QuestionsDomain.CreateQuestion(confirmQuest) select new { createResult, CreateQuestionResult }; var r = await _interpreter.Interpret(expr, ctx, dependencies); _dbContext.SaveChanges(); return(r.createResult.Match( created => (IActionResult)Ok(created.Title), notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Question could not be created."), invalidRequest => BadRequest("Invalid request."))); }
public async Task <IActionResult> CreateReply(int questionId) { //load from db var questionWriteContext = new QuestionWriteContext( new EFList <Tenant>(_dbContext.Tenant), new EFList <TenantUser>(_dbContext.TenantUser), new EFList <User>(_dbContext.User), new EFList <Post>(_dbContext.Post) ); var expr = from replyResult in QuestionsDomain.CreateReply(questionId, "123") select replyResult; CreateReplyResult.ICreateReplyResult result = await _interpreter.Interpret(expr, questionWriteContext, new object()); return(result.Match(created => Ok(created), notCreated => BadRequest(notCreated), invalidRequest => ValidationProblem() )); //return Ok(); }
public async Task <IActionResult> CreateTenantAsyncAndInviteAdmin([FromBody] CreateReplyCmd createReplyCmd) { var expr = from createReplyResult in QuestionsDomain.CreateReply(createReplyCmd.QuestionId, createReplyCmd.AuthorUserId, createReplyCmd.QuestionOwnerId, createReplyCmd.Body) let reply = createReplyResult.SafeCast <ReplyCreated>().Select(r => r) from checkLanguageResult in QuestionsDomain.CheckLanguage(createReplyCmd.Body) from sendQuestionOwnerAcknowledgementResult in QuestionsDomain.SendQuestionOwnerAcknowledgement(createReplyCmd.QuestionId, createReplyCmd.QuestionOwnerId) from sendReplyAuthorAcknowledgementResult in QuestionsDomain.SendReplyAuthorAcknowledgement(createReplyCmd.AuthorUserId, createReplyCmd.QuestionId, reply) select new { createReplyResult, sendQuestionOwnerAcknowledgementResult, sendReplyAuthorAcknowledgementResult }; var ctx = new QuestionsReadContext(new List <Post>()); var r = await _interpreter.Interpret(expr, ctx); await _dbContext.SaveChangesAsync(); return(r.createReplyResult.Match( created => (IActionResult)Ok("Reply added"), notCreated => BadRequest("Reply not added"))); }
public async Task <IActionResult> CreateQuestion([FromBody] CreateQuestionCmd cmd) { var questionDependencies = new QuestionDependencies(); //var questionWriteContext= await _db_Context.Question.ToList(); var questionWriteContext = new QuestionWriteContext(new EFList <Question>(_db_Context.Question)); var expr = from createQuestionResult in QuestionsDomain.CreateQuestion(cmd) /* from checkLanguageResult in QuestionsDomain.CheckLanguage(new CheckLanguageCmd(cmd.Body)) * from sendAckToQuestionOwnerCmd in QuestionsDomain.SendAckToQuestionOwner(new ReceivedAckSentToQuestionOwnerCmd(1, 2))*/ select createQuestionResult; var result = await _interpreter.Interpret(expr, questionWriteContext, new object()); // var result = await _interpreter.Interpret(expr, questionWriteContext, questionDependencies); //_db_Context.Question.Add(new DatabaseModel.Models.Question { QuestionId = 1, Title = cmd.Title, Body = cmd.Body, Tags = cmd.Tags }); // var question = await _dbContext.Question.Where(r => r.QuestionId == 1).SingleOrDefaultAsync(); // _dbContext.Question.Update(question); await _dbContext.SaveChangesAsync(); return(result.Match( create => (CreateTenantResult.TenantCreated)(IActionResult) Ok(create.QuestionId), notcreated => BadRequest("NotPosted"), invalidRequest => ValidationProblem() )); /* return result.createQuestionResult.Match( * created => (IActionResult)Ok(qe * QuestionPosted), * notCreated => StatusCode(StatusCodes.Status500InternalServerError, "Question could not be created."),//todo return 500 (), * invalidRequest => BadRequest("Invalid request.")); * /*return result.Match(created => (IActionResult)Ok(created), * notCreated => BadRequest(notCreated), * invalidRequest => ValidationProblem() * );*/ }