Skip to content

Commit

Permalink
Merge pull request #34 from Star-Academy/customException
Browse files Browse the repository at this point in the history
feat: add some custom exception and the test for class
  • Loading branch information
mahdizahedii2005 authored Sep 9, 2024
2 parents 130f409 + 607edb9 commit 3b646af
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 187 deletions.
97 changes: 71 additions & 26 deletions mohaymen-codestar-Team02/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using mohaymen_codestar_Team02.Dto;
using mohaymen_codestar_Team02.Dto.User;
using mohaymen_codestar_Team02.Dto.UserDtos;
using mohaymen_codestar_Team02.Dto.UserRole;
using mohaymen_codestar_Team02.Exception;
using mohaymen_codestar_Team02.Models;
using mohaymen_codestar_Team02.Services.Administration;

Expand All @@ -23,62 +25,105 @@ public AdminController(IAdminService adminService)
[HttpGet("users")]
public async Task<IActionResult> GetAllUsers([FromQuery] int pageNumber)
{
var response =
await _adminService.GetUsersPaginated(pageNumber);
ServiceResponse<List<GetUserDto>> response;
try
{
response =
await _adminService.GetUsersPaginated(pageNumber);
}
catch (ProgramException e)
{
response = new ServiceResponse<List<GetUserDto>>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpGet("users/{username}")]
public async Task<IActionResult> GetSingleUser(string? username)
{
var response =
await _adminService.GetUserByUsername(username);
ServiceResponse<GetUserDto> response;
try
{
response =
await _adminService.GetUserByUsername(username);
}
catch (ProgramException e)
{
response = new ServiceResponse<GetUserDto>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpPost("users")]
public async Task<IActionResult> CreateUser([FromBody] CreateUserDto request)
{
var user = new User
ServiceResponse<GetUserDto> response;
try
{
Username = request.Username,
FirstName = request.FirstName,
LastName = request.LastName,
Email = request.Email
};

var response =
await _adminService.CreateUser(user, request.Password, request.Roles);
var user = new User
{
Username = request.Username,
FirstName = request.FirstName,
LastName = request.LastName,
Email = request.Email
};

response =
await _adminService.CreateUser(user, request.Password, request.Roles);
}
catch (ProgramException e)
{
response = new ServiceResponse<GetUserDto>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpDelete("users/{username}")]
public async Task<IActionResult> DeleteUser(string username)
{
var user = new User
ServiceResponse<GetUserDto> response;
try
{
Username = username
};

var response =
await _adminService.DeleteUser(user);
var user = new User
{
Username = username
};

response =
await _adminService.DeleteUser(user);
}
catch (ProgramException e)
{
response = new ServiceResponse<GetUserDto>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpPut("users/update/{username}")]
public async Task<IActionResult> UpdateUser([FromBody] UpdateUserDto request, string username)
{
var updateUser = new User()
ServiceResponse<GetUserDto> response;
try
{
var updateUser = new User()
{
Username = username,
FirstName = request.FirstName,
LastName = request.LastName,
Email = request.Email
};

response = await _adminService.UpdateUser(updateUser);
}
catch (ProgramException e)
{
Username = username,
FirstName = request.FirstName,
LastName = request.LastName,
Email = request.Email
};
response = new ServiceResponse<GetUserDto>(null, ApiResponseType.InternalServerError, e.Message);
}

ServiceResponse<GetUserDto?> response = await _adminService.UpdateUser(updateUser);
return StatusCode((int)response.Type, response);
}

Expand Down
65 changes: 56 additions & 9 deletions mohaymen-codestar-Team02/Controllers/AnalystController.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,89 @@
using Microsoft.AspNetCore.Mvc;
using mohaymen_codestar_Team02.Dto;
using mohaymen_codestar_Team02.Dto.GraphDTO;
using mohaymen_codestar_Team02.Exception;
using mohaymen_codestar_Team02.Models;
using mohaymen_codestar_Team02.Services.AnalystService;
using mohaymen_codestar_Team02.Services.DataAdminService;

namespace mohaymen_codestar_Team02.Controllers;

public class AnalystController : ControllerBase
{
private readonly IAnalystService _analystService;

public AnalystController(IAnalystService analystService)
{
_analystService = analystService;
}

[HttpGet("Analyst/{vertexId}")]
public async Task<IActionResult> ExpandVertex([FromQuery] GraphQueryInfoDto graphQueryInfoDto, string vertexId)
{
var Response = await _analystService.GetTheVertexNeighbor(graphQueryInfoDto, vertexId);
return StatusCode((int)Response.Type, Response);
ServiceResponse<DisplayGraphDto> response;
try
{
response = await _analystService.GetTheVertexNeighbor(graphQueryInfoDto, vertexId);
}
catch (ProgramException e)
{
response = new ServiceResponse<DisplayGraphDto>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpPost("Analyst")]
public async Task<IActionResult> DisplayDataSetAsGraph([FromBody] FilterGraphDto filterGraphDto)
{
var response =
await _analystService.DisplayGeraphData(filterGraphDto.DatasetId, filterGraphDto.SourceIdentifier,
filterGraphDto.TargetIdentifier, filterGraphDto.VertexIdentifier, filterGraphDto.VertexAttributeValues,
filterGraphDto.EdgeAttributeValues);
response.Data.GraphId = filterGraphDto.DatasetId;
ServiceResponse<DisplayGraphDto> response;
try
{
response =
await _analystService.DisplayGeraphData(filterGraphDto.DatasetId, filterGraphDto.SourceIdentifier,
filterGraphDto.TargetIdentifier, filterGraphDto.VertexIdentifier,
filterGraphDto.VertexAttributeValues,
filterGraphDto.EdgeAttributeValues);
response.Data.GraphId = filterGraphDto.DatasetId;
}
catch (ProgramException e)
{
response = new ServiceResponse<DisplayGraphDto>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpGet("Analyst/Vertex/{id}")]
public IActionResult DisplayVertexAttributes(long id)
{
var response = _analystService.GetVertexAttributes(id);
ServiceResponse<List<GetAttributeDto>> response;
try
{
response = _analystService.GetVertexAttributes(id);
return StatusCode((int)response.Type, response);
}
catch (ProgramException e)
{
response = new ServiceResponse<List<GetAttributeDto>>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}

[HttpGet("Analyst/Edge/{id}")]
public IActionResult DisplayEdgeAttributes(long id)
{
var response = _analystService.GetEdgeAttributes(id);
ServiceResponse<List<GetAttributeDto>> response;
try
{
response = _analystService.GetEdgeAttributes(id);
}
catch (ProgramException e)
{
response = new ServiceResponse<List<GetAttributeDto>>(null, ApiResponseType.InternalServerError, e.Message);
}

return StatusCode((int)response.Type, response);
}
}
6 changes: 3 additions & 3 deletions mohaymen-codestar-Team02/Controllers/DataAdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DataAdminController : ControllerBase
{
private readonly IDataAdminService _dataAdminService;
private readonly IFileReader _fileReader;

public DataAdminController(IDataAdminService dataAdminService,
IFileReader fileReader)
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public IActionResult GetDataSetsList()
var response = _dataAdminService.DisplayDataSet();
return StatusCode((int)response.Type, response);
}

[HttpPost("DataSets/Graph")]
public async Task<IActionResult> DisplayDataSetAsGraph(GetGraphDto getGraphDto)
{
Expand All @@ -63,7 +63,7 @@ public async Task<IActionResult> DisplayVertexDetails(string objectId)
{
var respond = _dataAdminService.GetVertexDetail(objectId);
return StatusCode((int)respond.Type, respond);
}
}

[HttpGet("DataSets/Edges/{objectId}")]
public async Task<IActionResult> DisplayEdgeDetails(string objectId)
Expand Down
2 changes: 1 addition & 1 deletion mohaymen-codestar-Team02/Dto/GraphDto/FilterGraphDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FilterGraphDto
public long DatasetId { get; set; }
public string SourceIdentifier { get; set; }
public string TargetIdentifier { get; set; }
public string VertexIdentifier { get; set; }
public string VertexIdentifier { get; set; }
public Dictionary<string, string> VertexAttributeValues { get; set; }
public Dictionary<string, string> EdgeAttributeValues { get; set; }
}
2 changes: 1 addition & 1 deletion mohaymen-codestar-Team02/Dto/GraphDto/GetGraphDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public class GetGraphDto
public long DatasetId { get; set; }
public string SourceIdentifier { get; set; }
public string TargetIdentifier { get; set; }
public string VertexIdentifier { get; set; }
public string VertexIdentifier { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace mohaymen_codestar_Team02.Exception;

public class DatabaseExceptionCantFindDataBase() : ProgramException("cant find Database");
8 changes: 8 additions & 0 deletions mohaymen-codestar-Team02/Exception/ProgramException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace mohaymen_codestar_Team02.Exception;

public class ProgramException : System.Exception
{
public ProgramException(string? message) : base(message)
{
}
}
4 changes: 3 additions & 1 deletion mohaymen-codestar-Team02/Mapper/AutoMapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public AutoMapperProfile()
.ForMember(dest => dest.VertexEntity, opt =>
opt.MapFrom(src => src.VertexEntity));
CreateMap<DataGroup, GetDataGroupDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.DataGroupId));

CreateMap<EdgeEntity, GetEdgeEntityDto>();
CreateMap<VertexEntity, GetVertexEntityDto>();
CreateMap<EdgeAttribute, GetAttributeDto>();
Expand Down
Loading

0 comments on commit 3b646af

Please sign in to comment.