added CreatedBy to surveys

This commit is contained in:
Вячеслав 2025-04-19 00:16:07 +05:00
parent 3b6952364c
commit dbcdfac698
7 changed files with 443 additions and 22 deletions

View file

@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Services;
using SurveyBackend.DTOs;
using SurveyBackend.Mappers.UserDTOs;
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
namespace SurveyBackend.Controllers;
@ -16,6 +17,7 @@ public class AuthController : ControllerBase
_authorizationService = authorizationService;
}
[AllowAnonymous]
[HttpPost("login")]
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
{
@ -23,10 +25,12 @@ public class AuthController : ControllerBase
return Ok(new { token = token });
}
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
{
var token = await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
var token = await _authorizationService.RegisterUser(
UserRegistrationMapper.UserRegistrationToModel(registerData));
return Ok(new { token = token });
}
}

View file

@ -1,3 +1,5 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.DTOs.Survey;
using SurveyLib.Core.Models;
@ -17,6 +19,7 @@ public class SurveyController : ControllerBase
_surveyService = surveyService;
}
[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> Get()
{
@ -24,6 +27,7 @@ public class SurveyController : ControllerBase
return Ok(result);
}
[AllowAnonymous]
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
@ -31,18 +35,23 @@ public class SurveyController : ControllerBase
return result is not null ? Ok(result) : NotFound();
}
[Authorize]
[HttpPost]
public async Task<IActionResult> Post([FromBody] CreateSurveyDTO dto)
{
var userId = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value);
var survey = new Survey
{
Title = dto.Title,
Description = dto.Description,
CreatedBy = userId,
};
await _surveyService.AddSurveyAsync(survey);
return Ok();
}
[Authorize]
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{

View file

@ -2,6 +2,7 @@ using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using SurveyBackend.Core.Repositories;
using SurveyBackend.Core.Services;
using SurveyBackend.Infrastructure;
@ -62,7 +63,32 @@ public class Program
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Scheme = "bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JWT Authorization header using the Bearer scheme.",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = JwtBearerDefaults.AuthenticationScheme
}
},
Array.Empty<string>()
}
});
});
var app = builder.Build();