|
|
|
@ -2,6 +2,7 @@ using AutoMapper;
|
|
|
|
|
using CatalogService.Data;
|
|
|
|
|
using CatalogService.DTOs;
|
|
|
|
|
using CatalogService.Entities;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Shared.DTOs;
|
|
|
|
@ -20,19 +21,25 @@ public class ExercicesController : ControllerBase
|
|
|
|
|
_context = context;
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> Create([FromBody] CreateExerciceTemplateDto dto)
|
|
|
|
|
{
|
|
|
|
|
if (User.Identity.Name != "admin") return Forbid();
|
|
|
|
|
|
|
|
|
|
var exercice = _mapper.Map<Exercice>(dto);
|
|
|
|
|
_context.Exercices.Add(exercice);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
return CreatedAtAction(nameof(GetById), new { id = exercice.Id }, _mapper.Map<ExerciceTemplateDto>(exercice));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public async Task<IActionResult> Update(string id, [FromBody] UpdateExerciceTemplateDto dto)
|
|
|
|
|
{
|
|
|
|
|
if (User.Identity.Name != "admin") return Forbid();
|
|
|
|
|
|
|
|
|
|
var exercice = await _context.Exercices.FindAsync(id);
|
|
|
|
|
if (exercice == null) return NotFound();
|
|
|
|
|
|
|
|
|
@ -42,9 +49,12 @@ public class ExercicesController : ControllerBase
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
public async Task<IActionResult> Delete(string id)
|
|
|
|
|
{
|
|
|
|
|
if (User.Identity.Name != "admin") return Forbid();
|
|
|
|
|
|
|
|
|
|
var exercice = await _context.Exercices.FindAsync(id);
|
|
|
|
|
if (exercice == null) return NotFound();
|
|
|
|
|
|
|
|
|
@ -53,9 +63,12 @@ public class ExercicesController : ControllerBase
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public async Task<ActionResult<ExerciceTemplateDto>> GetById(string id)
|
|
|
|
|
{
|
|
|
|
|
if (User.Identity.Name != "admin") return Forbid();
|
|
|
|
|
|
|
|
|
|
var exercice = await _context.Exercices.FindAsync(id);
|
|
|
|
|
if (exercice == null) return NotFound();
|
|
|
|
|
|
|
|
|
|