Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend.aymen eliyas.assessment #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions backend/.vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ExpandedNodes": [
"",
"\\BackendAssessment",
"\\BackendAssessment\\Infrastructure"
],
"SelectedNode": "\\BackendAssessment\\Infrastructure\\Infrastructure.csproj",
"PreviewInSolutionExplorer": false
}
Binary file not shown.
Binary file not shown.
Empty file.
Binary file added backend/.vs/backend/v17/.wsuo
Binary file not shown.
Binary file added backend/.vs/slnx.sqlite
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="DTOs\" />
</ItemGroup>

</Project>
16 changes: 10 additions & 6 deletions backend/BackendAssessment/Application/Application.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -7,10 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1"/>
<PackageReference Include="File.TypeChecker" Version="4.0.0"/>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0"/>
<PackageReference Include="MediatR" Version="12.0.1"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5"/>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="File.TypeChecker" Version="4.0.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MediatR" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Domain.Entites;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Persistence.Repositories
{
public interface ICategoryRepository : IGenericRepository<Category>
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Persistence.Repositories
{
public interface IGenericRepository<T> where T : class
{
Task<T> Get(Guid id);
Task<List<T>> GetAll();
Task<T> Add(T entity);
Task<T> Update(T entity);
bool Exists(Guid Id);
void Delete(T entity);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Domain.Entites;
using Persistence.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Contracts
{
public interface IProductRepository : IGenericRepository<Product>
{

}
}
14 changes: 14 additions & 0 deletions backend/BackendAssessment/Application/Contracts/IUserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Domain.Entites;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Persistence.Repositories
{
public interface IUserRepository : IGenericRepository<User>
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Application.DTOs.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Category
{
public class CategoryRetriveDto : BaseEntityDto
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Application.DTOs.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Category
{
public class CreateCategoryDto : BaseEntityDto
{

}
}
16 changes: 16 additions & 0 deletions backend/BackendAssessment/Application/DTOs/Common/BaseEntityDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Common
{
public class BaseEntityDto
{
public int Id { get; set; }
public string name { get; set; }
public string description { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Application.DTOs.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Product
{
public class CreateProductDTO : BaseEntityDto
{
public bool isAvailable { get; set; }
public double pricing { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Application.DTOs.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Product
{
public class UpdateProductDto : BaseEntityDto
{
public bool isAvailable { get; set; }
public double pricing { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Product.Validator
{
public class CreateProductDtoValidator : AbstractValidator<CreateProductDTO>
{
public CreateProductDtoValidator()
{
RuleFor(dto => dto.name)
.NotEmpty()
.WithMessage("name is required.");

RuleFor(dto => dto.description)
.NotEmpty()
.WithMessage("Reciever description is required.");

RuleFor(dto => dto.pricing)
.NotEmpty()
.WithMessage("price is required.");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Application.DTOs.Category;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.Product
{
public class productRetrieveDTO
{
public string name { get; set; }
public string description { get; set; }
public string availability { get; set; }
public string pricing { get; set; }
public CategoryRetriveDto category { get; set; }

}
}
17 changes: 17 additions & 0 deletions backend/BackendAssessment/Application/DTOs/User/CreateUserDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.User
{
public class CreateUserDto
{
public string username { get; set; }
public string password { get; set; }
public string email { get; set; }
public bool isAdmin { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.DTOs.User.Validator
{
public class CreateUserDtoValidator : AbstractValidator<CreateUserDto>
{
public CreateUserDtoValidator() {

RuleFor(dto => dto.username)
.NotEmpty()
.WithMessage("name is required.");

RuleFor(dto => dto.email)
.NotEmpty()
.WithMessage("email is required.");

RuleFor(dto => dto.password)
.NotEmpty()
.WithMessage("password is required.");

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Application.DTOs.Category;
using Application.Features.Categories.Request.Command;
using AutoMapper;
using MediatR;
using Persistence.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Entites;

namespace Application.Features.Categories.Handler.Command
{
public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryCommand, CategoryRetriveDto>
{
private readonly ICategoryRepository _categoryRepository;
private readonly IMapper _mapper;

public CreateCategoryCommandHandler(ICategoryRepository i, IMapper mapper)
{
_categoryRepository = i;
_mapper = mapper;
}

public async Task<CategoryRetriveDto> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
{
var category = _mapper.Map<Category>(request);
var result = await _categoryRepository.Add(category);

var response = _mapper.Map<CategoryRetriveDto>(result);
return response;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Application.DTOs.Category;
using Application.Features.Categories.Request.Command;
using AutoMapper;
using Domain.Entites;
using MediatR;
using Persistence.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Application.Features.Categories.Handler.Command
{
public class UpdateCategoryCommandHandler : IRequestHandler<UpdateCategoryCommand, CategoryRetriveDto>
{
private readonly ICategoryRepository _categoryRepository;
private readonly IMapper _mapper;

public UpdateCategoryCommandHandler(ICategoryRepository i, IMapper mapper)
{
_categoryRepository = i;
_mapper = mapper;
}

public async Task<CategoryRetriveDto> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
{
var category = await _categoryRepository.Get(request.id)!;
if (category == null)
{
throw new Exception();
}

var categoryToUpdate = _mapper.Map(request, category );
var updatedCategory = await _categoryRepository.Update(categoryToUpdate);
return _mapper.Map<CategoryRetriveDto>(updatedCategory);
}


}
}
Loading