Genesis Game Server Project Setup
This commit is contained in:
62
GameServer/Controllers/UserController.cs
Normal file
62
GameServer/Controllers/UserController.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using GameServer.Data; // DbContext가 있는 폴더
|
||||
using GameServer.Models; // Model이 있는 폴더
|
||||
|
||||
namespace GameServer.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("myGame")] // 접속 주소: /myGame
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
// DB 연결 도구(Context)를 가져옵니다.
|
||||
public UserController(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// 모든 유저들의 캐릭터 목록
|
||||
[HttpGet("userCharacters")]
|
||||
public async Task<IActionResult> GetAllUserCharacters()
|
||||
{
|
||||
List<UserCharacterModel> list = await _context.UserCharacters.ToListAsync();
|
||||
|
||||
return Ok(new ApiResponse<List<UserCharacterModel>>
|
||||
{
|
||||
Data = list,
|
||||
Count = list.Count
|
||||
});
|
||||
}
|
||||
|
||||
// 특정 유저의 캐릭터 목록
|
||||
[HttpGet("userCharacters/{user_no}")]
|
||||
public async Task<IActionResult> GetUserCharacters(int user_no)
|
||||
{
|
||||
List<UserCharacterModel> list = await _context.UserCharacters
|
||||
.Where(x => x.UserNo == user_no)
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(new ApiResponse<List<UserCharacterModel>>
|
||||
{
|
||||
Data = list,
|
||||
Count = list.Count
|
||||
});
|
||||
}
|
||||
|
||||
// 새 캐릭터 저장하기
|
||||
[HttpPost("create")]
|
||||
public async Task<IActionResult> CreatePlayer([FromBody] UserCharacterModel newUserCharacter)
|
||||
{
|
||||
_context.UserCharacters.Add(newUserCharacter);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(new ApiResponse<UserCharacterModel>
|
||||
{
|
||||
Data = newUserCharacter,
|
||||
Message = "캐릭터 생성 완료!"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user