Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

학교 정보 입력 기능 개발 (#61) #62

Merged
merged 16 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ApplicationDbContext : DbContext
public DbSet<ChatLogsEntity> ChatLogsEntities { get; set; }
public DbSet<ChatParticipantsEntity> ChatParticipantsEntities { get; set; }
public DbSet<ChatRoomEntity> ChatRoomEntities { get; set; }

public DbSet<SchoolInformationEntity> SchoolInformationEntities { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
27 changes: 19 additions & 8 deletions server/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ public UserController(UserService userService)
}

[HttpPost]
public async Task<ActionResult<KeyValueErrorResponse>> Create([FromBody] CreateUserRequest body)
public async Task<ActionResult<object>> Create([FromBody] CreateUserRequest body)
{
try
{
bool result = await this._userService.Create(body);
if (result == false)
{
return BadRequest();
}

return Ok();
AuthResponse? result = await this._userService.Create(body);
return Ok(result);
}
catch (Exception error)
{
Expand Down Expand Up @@ -90,5 +85,21 @@ public async Task<ActionResult> Delete()

return Ok();
}

[Route("school")]
[HttpPost]
public async Task<ActionResult<KeyValueErrorResponse>> AddSchoolInfo([FromBody] CreateSchoolRequest body)
{
JwtSecurityToken jwtToken = HttpContext.GetJwtToken();
long id = long.Parse(jwtToken.GetClaimByType("id"));

bool result = await this._userService.AddSchoolInfo(id, body);
if (result == false)
{
return BadRequest();
}

return Ok();
}
}
}
17 changes: 17 additions & 0 deletions server/DTOs/CreateSchoolRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;

namespace server.DTOs;

public class CreateSchoolRequest
{
[Required]
public string School { get; set; }

[Required]
public string Department { get; set; }

[Required]
public string State { get; set; }

public int? Grade { get; set; }
}
22 changes: 22 additions & 0 deletions server/Entities/SchoolInformationEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace server.Entities
{
public class SchoolInformationEntity : BaseEntity
{
[Required]
public long User { get; set; }

[Required]
public string School { get; set; }

[Required]
public string Department { get; set; }

[Required]
public string State { get; set; }

public int? Grade { get; set; }

}
}
4 changes: 3 additions & 1 deletion server/Interface/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace server.Interface;

interface IUserService
{
public Task<bool> Create(CreateUserRequest body);
public Task<AuthResponse> Create(CreateUserRequest body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인필요

Interface 상의 Return Type은 Task이나, 구현된 Controller에서는 Task<ActionResult> 입니다.

public Task<bool> AddSchoolInfo(long id, CreateSchoolRequest body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인필요

7라인과 유사합니다


}

29 changes: 26 additions & 3 deletions server/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using server.DTOs;
using server.Entities;
Expand All @@ -11,18 +12,20 @@ namespace server.Services;
public class UserService : IUserService
{
private readonly ApplicationDbContext _context;
private readonly AuthService _authService;

public UserService(ApplicationDbContext context)
public UserService(ApplicationDbContext context, AuthService auth)
{
_context = context;
_authService = auth;
}

private bool UserEntityExists(long id)
{
return (_context.Users?.Any(e => e.Id == id)).GetValueOrDefault();
}

public async Task<bool> Create(CreateUserRequest body)
public async Task<AuthResponse?> Create(CreateUserRequest body)
{
UserEntity userEntity = new UserEntity
{
Expand All @@ -37,7 +40,12 @@ public async Task<bool> Create(CreateUserRequest body)
};
this._context.Users.Add(userEntity);
await this._context.SaveChangesAsync();
return true;

return new AuthResponse
{
access_token = _authService.GenerateAccessToken(userEntity.Id),
refresh_token = _authService.GenerateRefreshToken(userEntity.Id)
};
}

public async Task<GetUserResponse?> Read(long id)
Expand Down Expand Up @@ -93,4 +101,19 @@ public async Task Delete(long id)
_context.Users.Remove(user);
await _context.SaveChangesAsync();
}
public async Task<bool> AddSchoolInfo(long id, CreateSchoolRequest body)
{
SchoolInformationEntity schoolInformationEntity = new SchoolInformationEntity
{
User = id,
School = body.School,
Department = body.Department,
State = body.State,
Grade = body.Grade
};

this._context.SchoolInformationEntities.Add(schoolInformationEntity);
await this._context.SaveChangesAsync();
return true;
}
}