コード例 #1
0
        public IActionResult GetUserInfo([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userInfos = from Userinfo in _context.UserInfos
                            join comments in _context.Comments on Userinfo equals comments.CommentingUser into userComments
                            join post in _context.Posts on Userinfo equals post.PostingUser into userPosts
                            select new UserInfo()
            {
                Comments         = new List <Comment>(userComments),
                Name             = Userinfo.Name,
                NumberOfComments = Userinfo.NumberOfComments,
                NumberOfPosts    = Userinfo.NumberOfPosts,
                Posts            = (ICollection <Post>)userPosts,
                ProfilPictureUrl = Userinfo.ProfilPictureUrl,
                RegisterDate     = Userinfo.RegisterDate,
                UserInfoID       = Userinfo.UserInfoID,
                Username         = Userinfo.Username
            };
            UserinfoDTO dto = AutoMapper.Mapper.Map <UserInfo, UserinfoDTO>(userInfos.FirstOrDefault(c => c.UserInfoID == id));

            if (dto is null)
            {
                return(NotFound());
            }

            return(Ok(dto));
        }
コード例 #2
0
        public async Task <IActionResult> PutUserInfo([FromRoute] int id, [FromBody] UserinfoDTO userInfo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != userInfo.UserInfoID)
            {
                return(BadRequest());
            }
            UserInfo editedUser = AutoMapper.Mapper.Map <UserinfoDTO, UserInfo>(userInfo);

            _context.Entry(editedUser).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserInfoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> PostUserInfo([FromBody] UserinfoDTO userInfo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (userInfo.UserInfoID != 0)
            {
                return(BadRequest());
            }
            _context.UserInfos.Add(AutoMapper.Mapper.Map <UserinfoDTO, UserInfo>(userInfo));
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetUserInfo", new { id = userInfo.UserInfoID }, userInfo));
        }