コード例 #1
0
        public IActionResult Post2([FromForm] PoemViewModel data)
        {
            if (!ModelState.IsValid)
            {
                //[ApiController] 's default model state invalid got suppresssed: SuppressModelStateInvalidFilter
                //see Startup.cs and https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#binding-source-parameter-inference
                //return BadRequest(ModelState); //old version response: SerializableError class

                //after 2.2 (include 2.2)
                return(BadRequest(new ValidationProblemDetails(ModelState)
                {
                    Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1",
                    //Instance =
                    //todo learn more about ValidationProblemDetails
                }));
            }

            if (data.PoemId > 0)
            {
                return(BadRequest());
            }

            var newId = 1;

            if (Items.Count > 0)
            {
                newId = Items.Max(r => r.PoemId) + 1;
            }

            data.PoemId = newId;

            Items.Add(data);

            return(CreatedAtAction(nameof(Post), new { id = newId }, data));
        }
コード例 #2
0
        public async Task <IActionResult> Post([FromBody] PoemViewModel poem)
        {
            Poet currentPoet = await CurrentPoet();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _appDbContext.Poems.AddAsync(new Poem { Title         = poem.Title, Body = poem.Body,
                                                          PostToInspire = poem.PostToInspire, Poet = currentPoet });

            _appDbContext.SaveChanges();

            return(new OkObjectResult("Poem created"));
        }
コード例 #3
0
        public IActionResult Put(int id, PoemViewModel data)
        {
            if (!ModelState.IsValid)
            {
                //[ApiController] 's default model state invalid got suppresssed: SuppressModelStateInvalidFilter
                //see Startup.cs and https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#binding-source-parameter-inference
                //return BadRequest(ModelState); //old version response: SerializableError class

                //after 2.2 (include 2.2)
                return(BadRequest(new ValidationProblemDetails(ModelState)
                {
                    Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1",
                    //Instance =
                    //todo learn more about ValidationProblemDetails
                }));
            }

            if (data.PoemId != id)
            {
                return(BadRequest());
            }

            var row = Items.FirstOrDefault(r => r.PoemId == id);

            if (row == null)
            {
                return(NotFound());
            }

            row.Title      = data.Title;
            row.Author     = data.Author;
            row.Content    = data.Content;
            row.Decription = data.Decription;
            row.CreateDate = data.CreateDate;

            return(Ok(data));
        }
コード例 #4
0
        public ActionResult Poem(int Id)
        {
            if (Request.IsAuthenticated)
            {
                string userId = User.Identity.GetUserId();
                BPUser bpUser = _context.BPUsers.Where(s => s.AppId == userId).SingleOrDefault();
                System.Web.HttpContext.Current.Session["UserName"] = bpUser.FirstName + " " + bpUser.LastName;

                // only mark as read if not already in the database
                if (!_context.BPUserPoems.Any(p => p.PoemId == Id && p.BPUserId == bpUser.Id))
                {
                    // add poem to list of read poems
                    BPUserPoems bpup = new BPUserPoems {
                        BPUserId = bpUser.Id, PoemId = Id
                    };
                    _context.BPUserPoems.Add(bpup);
                    _context.SaveChanges();
                }
            }

            PoemViewModel poemViewModel = new PoemViewModel(Id);

            return(View(poemViewModel.FileNameView, "_Layout", poemViewModel));
        }