示例#1
0
        public async Task <IActionResult> Create(CreateChapterModel model)
        {
            model.Chapter.PictureURL = UploadImage(model.Image);
            var user = await _userManager.GetUserAsync(User);

            var id      = TempData["Fiction"].ToString();
            var fiction = _context.Fictions.Single(i => i.Id == id);

            fiction.Chapters         = _context.Chapters.Where(chap => chap.FictionId == id).ToList();
            fiction.LastModifiedDate = DateTime.Now;


            model.Chapter.FictionId = fiction.Id;

            var chapterCounter = fiction.Chapters.Count;

            model.Chapter.ChapterNumber += chapterCounter;
            model.Chapter.Rating         = 0;



            fiction.LastModifiedDate = DateTime.Now;

            fiction.Chapters.Add(model.Chapter);



            _chapterRepository.Chapters.Append(model.Chapter);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Chapter", new { Id = model.Chapter.Id }));
        }
        public IActionResult Create([FromBody] CreateChapterModel model)
        {
            // map model to entity
            var chapter = _mapper.Map <Chapter>(model);

            try
            {
                // create chapter
                _chapterService.Create(chapter);
                return(Ok(new {
                    data = chapter,
                    message = "Successfully created chapter."
                }));
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }