Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ public interface IRepository<T> where T: BaseEntity
Task<IEnumerable<T>> GetAllAsync();

Task<T> GetByIdAsync(Guid id);

Task<bool> RemoveAsync(Guid id);
Task<Guid> AddAsync(T data);
Task<T> UpdateAsync(T data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,41 @@ public Task<T> GetByIdAsync(Guid id)
{
return Task.FromResult(Data.FirstOrDefault(x => x.Id == id));
}

public Task<bool> RemoveAsync(Guid id)
{
var list = Data.ToList();
var removeCount = list.RemoveAll(x => x.Id == id);
Data = list;
return Task.FromResult(removeCount > 0);
}

public Task<Guid> AddAsync(T data)
{
if(data == null)
throw new ArgumentNullException(nameof(data));

data.Id = Guid.NewGuid();
Data = Data.Append(data);

return Task.FromResult(data.Id);
}

public Task<T> UpdateAsync(T data)
{
if(data == null)
throw new ArgumentNullException(nameof(data));

var list = Data.ToList();
var index = list.FindIndex(x => x.Id == data.Id);

if(index < 0)
throw new KeyNotFoundException(data.Id.ToString());

list[index] = data;
Data = list;

return Task.FromResult(data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,60 @@ public async Task<ActionResult<EmployeeResponse>> GetEmployeeByIdAsync(Guid id)

return employeeModel;
}

/// <summary>
/// Создать нового сотрудника
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<Guid>> CreateEmployeeAsync([FromBody] EmployeeAddRequest request)
{
var newEmployee = new Employee
{
Email = request.Email,
FirstName = request.FirstName,
LastName = request.LastName,
Roles = new()
};

var guid = await _employeeRepository.AddAsync(newEmployee);

return Ok(guid);
}

/// <summary>
/// Обновить данные переданного сотрудника
/// </summary>
/// <returns></returns>
[HttpPut]
public async Task<ActionResult<EmployeeResponse>> UpdateEmployeeAsync([FromBody] EmployeeUpdateRequest request)
{
var employee = await _employeeRepository.GetByIdAsync(request.Id);

if (employee == null)
return NotFound();

if (request.Email != null)
employee.Email = request.Email;

if (request.FirstName != null)
employee.FirstName = request.FirstName;

if (request.LastName != null)
employee.LastName = request.LastName;

await _employeeRepository.UpdateAsync(employee);
return await GetEmployeeByIdAsync(employee.Id);
}

/// <summary>
/// Удалить данные сотрудника по Id
/// </summary>
/// <returns></returns>
[HttpDelete("{id:guid}")]
public async Task<ActionResult<bool>> DeleteEmployeeAsync(Guid id)
{
return Ok(await _employeeRepository.RemoveAsync(id));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -13,7 +14,7 @@ namespace PromoCodeFactory.WebHost.Controllers
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
public class RolesController
public class RolesController : ControllerBase
{
private readonly IRepository<Role> _rolesRepository;

Expand All @@ -22,6 +23,28 @@ public RolesController(IRepository<Role> rolesRepository)
_rolesRepository = rolesRepository;
}

/// <summary>
/// Получить данные роли по Id
/// </summary>
/// <returns></returns>
[HttpGet("{id:guid}")]
public async Task<ActionResult<RoleItemResponse>> GetRoleByIdAsync(Guid id)
{
var role = await _rolesRepository.GetByIdAsync(id);

if (role == null)
return NotFound();

var roleModel = new RoleItemResponse
{
Id = role.Id,
Name = role.Name,
Description = role.Description,
};

return Ok(roleModel);
}

/// <summary>
/// Получить все доступные роли сотрудников
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace PromoCodeFactory.WebHost.Models;

public class EmployeeAddRequest
{
public string FirstName { get; set; }

public string LastName { get; set; }

public string Email { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using PromoCodeFactory.Core.Domain.Administration;

namespace PromoCodeFactory.WebHost.Models;

public class EmployeeUpdateRequest
{
public Guid Id { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Email { get; set; }
}