CRUD internshipType
This commit is contained in:
parent
d0972673d8
commit
78ebdbe33d
@ -1,8 +1,8 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using InternshipSystem.Api.Queries;
|
||||||
using InternshipSystem.Api.Security;
|
using InternshipSystem.Api.Security;
|
||||||
using InternshipSystem.Core.Entity.Internship;
|
using InternshipSystem.Core.Entity.Internship;
|
||||||
using InternshipSystem.Repository;
|
using InternshipSystem.Repository;
|
||||||
@ -14,21 +14,21 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
namespace InternshipSystem.Api.Controllers
|
namespace InternshipSystem.Api.Controllers
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("internshipType")]
|
[Route("internshipTypes")]
|
||||||
public class InternshipTypesController : ControllerBase
|
public class InternshipTypesController : ControllerBase
|
||||||
{
|
{
|
||||||
|
private InternshipDbContext Context { get; }
|
||||||
|
|
||||||
public InternshipTypesController(InternshipDbContext context)
|
public InternshipTypesController(InternshipDbContext context)
|
||||||
{
|
{
|
||||||
Context = context;
|
Context = context;
|
||||||
}
|
}
|
||||||
private InternshipDbContext Context { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get static page
|
/// Get static page
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>List of internship types for edition</returns>
|
/// <returns>List of internship types for edition</returns>
|
||||||
[HttpGet]
|
[HttpGet("forCurrentEdition")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
@ -48,5 +48,97 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
|
|
||||||
return Ok(edition.AvailableInternshipTypes);
|
return Ok(edition.AvailableInternshipTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("{internshipTypeId}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<InternshipType>> GetInternshipType(long internshipTypeId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return await Context.InternshipTypes.FindAsync(internshipTypeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<IList<InternshipType>>> SearchInternshipTypes([FromBody] InternshipTypeSearchQuery searchQuery, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return await Context.InternshipTypes
|
||||||
|
.Where(t => string.IsNullOrEmpty(searchQuery.Type) || t.Type.Contains(searchQuery.Type))
|
||||||
|
.OrderBy(t => t.Type)
|
||||||
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
||||||
|
.Take(searchQuery.PerPage)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult> UpdateInternshipType([FromBody] InternshipTypeFrom internshipTypeFrom, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var validator = new InternshipTypeFrom.Validator();
|
||||||
|
var validationResult = await validator.ValidateAsync(internshipTypeFrom, cancellationToken);
|
||||||
|
|
||||||
|
if (!validationResult.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(validationResult.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (internshipTypeFrom.Id.HasValue)
|
||||||
|
{
|
||||||
|
var internshipType = await Context.InternshipTypes.FindAsync(internshipTypeFrom.Id.Value);
|
||||||
|
|
||||||
|
if (internshipType == null)
|
||||||
|
{
|
||||||
|
return NotFound($"Internship type with id {internshipTypeFrom.Id} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
internshipType.Type = string.IsNullOrEmpty(internshipTypeFrom.Type) ? internshipType.Type : internshipTypeFrom.Type;
|
||||||
|
internshipType.Description = string.IsNullOrEmpty(internshipTypeFrom.Description) ? internshipType.Description : internshipTypeFrom.Description;
|
||||||
|
internshipType.DescriptionEng = string.IsNullOrEmpty(internshipTypeFrom.DescriptionEng) ? internshipType.DescriptionEng : internshipTypeFrom.DescriptionEng;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var newInternshipType = new InternshipType
|
||||||
|
{
|
||||||
|
Type = internshipTypeFrom.Type,
|
||||||
|
Description = internshipTypeFrom.Description,
|
||||||
|
DescriptionEng = internshipTypeFrom.DescriptionEng,
|
||||||
|
};
|
||||||
|
|
||||||
|
await Context.InternshipTypes.AddAsync(newInternshipType, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return Ok($"Internship type updated successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{internshipTypeId}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult> DeleteInternshipType(long internshipTypeId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var internshipTypeToDelete = await Context.InternshipTypes
|
||||||
|
.FirstOrDefaultAsync(t => t.Id.Equals(internshipTypeId), cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
if (internshipTypeToDelete == null)
|
||||||
|
{
|
||||||
|
return NotFound($"Internship type with id: {internshipTypeId} does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
Context.InternshipTypes.Attach(internshipTypeToDelete);
|
||||||
|
Context.InternshipTypes.Remove(internshipTypeToDelete);
|
||||||
|
await Context.SaveChangesAsync(cancellationToken);
|
||||||
|
return Ok($"Internship type with id: {internshipTypeId} deleted successfully");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -58,6 +58,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[HttpPut]
|
[HttpPut]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult> UpdateStaticPage([FromBody] StaticPageForm staticPageForm, CancellationToken cancellationToken)
|
public async Task<ActionResult> UpdateStaticPage([FromBody] StaticPageForm staticPageForm, CancellationToken cancellationToken)
|
||||||
@ -70,17 +71,6 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
return BadRequest(validationResult.ToString());
|
return BadRequest(validationResult.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(staticPageForm.AccessName))
|
|
||||||
{
|
|
||||||
var pageWithSameAccessName = await Context.StaticPages
|
|
||||||
.FirstOrDefaultAsync(sp => sp.AccessName.ToLower().Trim().Equals(staticPageForm.AccessName.ToLower().Trim()), cancellationToken: cancellationToken);
|
|
||||||
|
|
||||||
if (pageWithSameAccessName != null)
|
|
||||||
{
|
|
||||||
return BadRequest($"Static page with access name: {staticPageForm.AccessName} already exist");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (staticPageForm.Id.HasValue)
|
if (staticPageForm.Id.HasValue)
|
||||||
{
|
{
|
||||||
var pageToUpdate = await Context.StaticPages.FindAsync(staticPageForm.Id);
|
var pageToUpdate = await Context.StaticPages.FindAsync(staticPageForm.Id);
|
||||||
@ -90,6 +80,17 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
return NotFound($"Static page with id: {staticPageForm.Id} does not exist");
|
return NotFound($"Static page with id: {staticPageForm.Id} does not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(staticPageForm.AccessName))
|
||||||
|
{
|
||||||
|
var pageWithSameAccessName = await Context.StaticPages
|
||||||
|
.FirstOrDefaultAsync(sp => sp.AccessName.ToLower().Trim().Equals(staticPageForm.AccessName.ToLower().Trim()), cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
if (pageWithSameAccessName != null && !pageWithSameAccessName.Id.Equals(pageToUpdate.Id))
|
||||||
|
{
|
||||||
|
return BadRequest($"Static page with access name: {staticPageForm.AccessName} already exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pageToUpdate.AccessName = string.IsNullOrEmpty(staticPageForm.AccessName) ? pageToUpdate.AccessName : staticPageForm.AccessName;
|
pageToUpdate.AccessName = string.IsNullOrEmpty(staticPageForm.AccessName) ? pageToUpdate.AccessName : staticPageForm.AccessName;
|
||||||
pageToUpdate.Title = string.IsNullOrEmpty(staticPageForm.Title) ? pageToUpdate.Title : staticPageForm.Title;
|
pageToUpdate.Title = string.IsNullOrEmpty(staticPageForm.Title) ? pageToUpdate.Title : staticPageForm.Title;
|
||||||
pageToUpdate.TitleEng = string.IsNullOrEmpty(staticPageForm.TitleEng) ? pageToUpdate.TitleEng : staticPageForm.TitleEng;
|
pageToUpdate.TitleEng = string.IsNullOrEmpty(staticPageForm.TitleEng) ? pageToUpdate.TitleEng : staticPageForm.TitleEng;
|
||||||
@ -98,6 +99,14 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
var pageWithSameAccessName = await Context.StaticPages
|
||||||
|
.FirstOrDefaultAsync(sp => sp.AccessName.ToLower().Trim().Equals(staticPageForm.AccessName.ToLower().Trim()), cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
if (pageWithSameAccessName != null)
|
||||||
|
{
|
||||||
|
return BadRequest($"Static page with access name: {staticPageForm.AccessName} already exist");
|
||||||
|
}
|
||||||
|
|
||||||
var newStaticPage = new StaticPage
|
var newStaticPage = new StaticPage
|
||||||
{
|
{
|
||||||
AccessName = staticPageForm.AccessName.ToLower().Trim(),
|
AccessName = staticPageForm.AccessName.ToLower().Trim(),
|
||||||
@ -118,6 +127,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[HttpDelete("{accessName}")]
|
[HttpDelete("{accessName}")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult> DeleteStaticPage(string accessName, CancellationToken cancellationToken)
|
public async Task<ActionResult> DeleteStaticPage(string accessName, CancellationToken cancellationToken)
|
||||||
|
28
src/InternshipSystem.Api/Queries/InternshipTypeFrom.cs
Normal file
28
src/InternshipSystem.Api/Queries/InternshipTypeFrom.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
namespace InternshipSystem.Api.Queries
|
||||||
|
{
|
||||||
|
public class InternshipTypeFrom
|
||||||
|
{
|
||||||
|
public long? Id { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public string DescriptionEng { get; set; }
|
||||||
|
|
||||||
|
public class Validator : AbstractValidator<InternshipTypeFrom>
|
||||||
|
{
|
||||||
|
public Validator()
|
||||||
|
{
|
||||||
|
RuleFor(t => t.Id).NotNull()
|
||||||
|
.When(t =>
|
||||||
|
string.IsNullOrEmpty(t.Description) || string.IsNullOrEmpty(t.Type) || string.IsNullOrEmpty(t.DescriptionEng));
|
||||||
|
RuleFor(t => t.Type).NotEmpty()
|
||||||
|
.When(t => !t.Id.HasValue);
|
||||||
|
RuleFor(t => t.Description).NotEmpty()
|
||||||
|
.When(t => !t.Id.HasValue);
|
||||||
|
RuleFor(t => t.DescriptionEng).NotEmpty()
|
||||||
|
.When(t => !t.Id.HasValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
using InternshipSystem.Api.Queries.SearchQuery;
|
||||||
|
|
||||||
|
namespace InternshipSystem.Api.Controllers
|
||||||
|
{
|
||||||
|
public class InternshipTypeSearchQuery : SearchQuery
|
||||||
|
{
|
||||||
|
public string Type { get; set; } = "";
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using InternshipSystem.Core;
|
|
||||||
|
|
||||||
namespace InternshipSystem.Api.Queries
|
namespace InternshipSystem.Api.Queries
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user