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
Binary file added .vs/ASP.NET/v17/.wsuo
Binary file not shown.
27 changes: 27 additions & 0 deletions .vs/ASP.NET/v17/DocumentLayout.backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\psizh\\source\\repos\\ASP.NET\\",
"Documents": [],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 249,
"SelectedChildIndex": -1,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:0:0:{1c64b9c2-e352-428e-a56d-0ace190b99a6}"
},
{
"$type": "Bookmark",
"Name": "ST:128:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
}
]
}
]
}
]
}
23 changes: 23 additions & 0 deletions .vs/ASP.NET/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\Sharpey\\source\\repos\\ASP.NET\\",
"Documents": [],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 249,
"SelectedChildIndex": -1,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:0:0:{1c64b9c2-e352-428e-a56d-0ace190b99a6}"
}
]
}
]
}
]
}
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\AspApiBasic.sln",
"PreviewInSolutionExplorer": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public interface IRepository<T> where T: BaseEntity
Task<IEnumerable<T>> GetAllAsync();

Task<T> GetByIdAsync(Guid id);
Task<T> AddAsync(T entity);
Task<T> UpdateAsync(T entity);
Task DeleteAsync(Guid id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,55 @@
using PromoCodeFactory.Core.Domain;
namespace PromoCodeFactory.DataAccess.Repositories
{
public class InMemoryRepository<T>: IRepository<T> where T: BaseEntity
public class InMemoryRepository<T> : IRepository<T> where T : BaseEntity
{
protected IEnumerable<T> Data { get; set; }
protected static List<T> Data = new();

public InMemoryRepository(IEnumerable<T> data)
{
Data = data;
if(!Data.Any() && data != null)
{
Data = data.ToList();
}
}

public Task<IEnumerable<T>> GetAllAsync()
{
return Task.FromResult(Data);
return Task.FromResult(Data.AsEnumerable());
}

public Task<T> GetByIdAsync(Guid id)
{
return Task.FromResult(Data.FirstOrDefault(x => x.Id == id));
}

public Task<T> AddAsync(T entity)
{
if(entity == null)
throw new ArgumentNullException(nameof(entity));
entity.Id = Guid.NewGuid();
Data.Add(entity);
return Task.FromResult(entity);
}

public Task<T> UpdateAsync(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
var index = Data.FindIndex(x => x.Id == entity.Id);
if (index == -1)
throw new KeyNotFoundException($"Сущность с таким {entity.Id} не найдена");
Data[index] = entity;
return Task.FromResult(entity);
}

public Task DeleteAsync(Guid id)
{
var entity = Data.FirstOrDefault(x => x.Id == id);
if (entity == null)
throw new KeyNotFoundException($"Сущность с таким {id} не найдена");
Data.Remove(entity);
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,69 @@ public async Task<ActionResult<EmployeeResponse>> GetEmployeeByIdAsync(Guid id)

return employeeModel;
}

/// <summary>
/// Создать нового сотрудника
/// </summary>
[HttpPost]
public async Task<ActionResult<EmployeeResponse>> CreateEmployeeAsync([FromBody] EmployeeResponse employeeModel)
{

string[] name = employeeModel.FullName.Split(' ');
var employee = new Employee()
{
Id = employeeModel.Id,
Email = employeeModel.Email,
Roles = employeeModel.Roles.Select(x => new Role()
{
Name = x.Name,
Description = x.Description
}).ToList(),
FirstName = name[0],
LastName = name[1],
AppliedPromocodesCount = employeeModel.AppliedPromocodesCount
};

await _employeeRepository.AddAsync(employee);

return employeeModel;
}

/// <summary>
/// Обновить данные сотрудника
/// </summary>
[HttpPut("{id:guid}")]
public async Task<IActionResult> UpdateEmployeeAsync(Guid id, [FromBody] Employee newEmployee)
{

var employee = await _employeeRepository.GetByIdAsync(id);
if (employee == null)
return NotFound();

employee.FirstName = newEmployee.FirstName;
employee.LastName = newEmployee.LastName;
employee.Email = newEmployee.Email;
employee.Roles = newEmployee.Roles;
employee.AppliedPromocodesCount = newEmployee.AppliedPromocodesCount;

await _employeeRepository.UpdateAsync(newEmployee);
return Ok();

}

/// <summary>
/// Удалить сотрудника
/// </summary>
[HttpDelete("{id:guid}")]
public async Task<IActionResult> DeleteEmployeeAsync(Guid id)
{

var employee = await _employeeRepository.GetByIdAsync(id);
if (employee == null)
return NotFound();
await _employeeRepository.DeleteAsync(id);
return Ok();

}
}
}