Skip to content

Commit

Permalink
Merge pull request #6 from WesleyJoseSantos/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
wjsan authored Jan 8, 2023
2 parents 82e8197 + 2191c62 commit 04c89e6
Show file tree
Hide file tree
Showing 26 changed files with 447 additions and 75 deletions.
4 changes: 2 additions & 2 deletions UsersDBApi/Domain/DTOs/UserCredentialsDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class UserCredentialsDTO

public string Password { get; set;}

public UserCredentialsDTO(string userName, string password)
public UserCredentialsDTO(string email, string password)
{
Email = userName;
Email = email;
Password = password;
}
}
Expand Down
9 changes: 9 additions & 0 deletions UsersDBApi/Domain/Database/IConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Data;

namespace UsersDBApi.Domain.Database
{
public interface IConnection
{
IDbConnection Get(string name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UsersDBApi.Domain.Errors
{
public class EmailAlreadyExists : IBaseError
public class EmailAlreadyExistsError : IBaseError
{
public int ErrorCode => 0x6001;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UsersDBApi.Domain.Errors
{
public class InvalidPassword : IBaseError
public class InvalidPasswordError : IBaseError
{
public int ErrorCode => 0x7003;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UsersDBApi.Domain.Errors
{
public class InvalidPhoneNumber : IBaseError
public class InvalidPhoneNumberError : IBaseError
{
public int ErrorCode => 0x7002;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UsersDBApi.Domain.Errors
{
public class NameAlreadyExists : IBaseError
public class NameAlreadyExistsError : IBaseError
{
public int ErrorCode => 0x6000;

Expand Down
File renamed without changes.
15 changes: 5 additions & 10 deletions UsersDBApi/Infra/Database/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Configuration;
using System.Data.SqlClient;
using System.Security.Cryptography.X509Certificates;
using System.Data.Common;
using UsersDBApi.Domain.Database;
using System.Data;

namespace UsersDBApi.Infra.Database
{
public class Connection
public class Connection : IConnection
{
static public DbConnection Get(string name)
public IDbConnection Get(string name)
{
string connectionString = ConfigurationManager.ConnectionStrings[name].ConnectionString;
return new SqlConnection(connectionString);
Expand Down
23 changes: 15 additions & 8 deletions UsersDBApi/Infra/Repositories/UsersRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@
using UsersDBApi.Domain.Errors;
using UsersDBApi.Domain.DTOs;
using UsersDBApi.Domain.repositories;
using UsersDBApi.Infra.Database;
using UsersDBApi.Lib;
using Dapper;
using UsersDBApi.Infra.Database.Models;
using UsersDBApi.Domain.Database;

namespace UsersDBApi.Infra.Repositories
{
public class UsersRepository : IUsersRepository
{
private IConnection connection;

public UsersRepository(IConnection connection)
{
this.connection = connection;
}

public Either<IBaseError, UserModel> CreateUser(UserDTO user)
{
using (IDbConnection db = Connection.Get("usersdb"))
using (IDbConnection db = connection.Get("usersdb"))
{
var model = UserModel.Create(user);

var name = db.Query<UserDTO>($"SELECT * FROM users WHERE Name = '{user.Name}'");
if(name.Count() > 0 )
{
return new NameAlreadyExists();
return new NameAlreadyExistsError();
}

var email = db.Query<UserModel>($"SELECT * FROM users WHERE Email = '{user.Email}'");
if(email.Count() > 0 )
{
return new EmailAlreadyExists();
return new EmailAlreadyExistsError();
}

db.Execute($"INSERT INTO users (Name, Email, Phone, Password, Level, CreatedAt) Values(@Name, @Email, @Phone, @Password, @Level, @CreatedAt)", model);
Expand All @@ -38,7 +45,7 @@ public Either<IBaseError, UserModel> CreateUser(UserDTO user)

public Either<IBaseError, List<UserModel>> GetAllUsers()
{
using (IDbConnection db = Connection.Get("usersdb"))
using (IDbConnection db = connection.Get("usersdb"))
{
var result = db.Query<UserModel>($"SELECT * FROM users");
if(result.Count() > 0)
Expand All @@ -54,7 +61,7 @@ public Either<IBaseError, List<UserModel>> GetAllUsers()

public Either<IBaseError, UserModel> GetUserByEmail(string email)
{
using (IDbConnection db = Connection.Get("usersdb"))
using (IDbConnection db = connection.Get("usersdb"))
{
var result = db.Query<UserModel>($"SELECT * FROM users WHERE Email = '{email}'");
if(result.Count() > 0)
Expand All @@ -70,7 +77,7 @@ public Either<IBaseError, UserModel> GetUserByEmail(string email)

public Either<IBaseError, UserModel> GetUserById(int id)
{
using (IDbConnection db = Connection.Get("usersdb"))
using (IDbConnection db = connection.Get("usersdb"))
{
var result = db.Query<UserModel>($"SELECT * FROM users WHERE Id = {id}");
if (result.Count() > 0)
Expand All @@ -86,7 +93,7 @@ public Either<IBaseError, UserModel> GetUserById(int id)

public Either<IBaseError, List<UserModel>> GetUsersByName(string name)
{
using (IDbConnection db = Connection.Get("usersdb"))
using (IDbConnection db = connection.Get("usersdb"))
{
var result = db.Query<UserModel>($"SELECT * FROM users WHERE CHARINDEX('{name}', Name) > 0");
if (result.Count() > 0)
Expand Down
4 changes: 2 additions & 2 deletions UsersDBApi/Infra/Usecases/Users/CreateUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public Either<IBaseError, UserModel> Handle(UserDTO user)
var phoneRegex = new Regex("^\\+?[1-9][0-9]{7,14}$");
if (user.Phone == null || !phoneRegex.IsMatch(user.Phone))
{
return new InvalidPhoneNumber();
return new InvalidPhoneNumberError();
}

if(user.Password == null || user.Password.Length < 8)
{
return new InvalidPassword();
return new InvalidPasswordError();
}

try
Expand Down
13 changes: 7 additions & 6 deletions UsersDBApi/UsersDBApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,24 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Domain\Database\IConnection.cs" />
<Compile Include="Domain\DTOs\UserCredentialsDTO.cs" />
<Compile Include="Domain\Errors\EmailAlreadyExists.cs" />
<Compile Include="Domain\Errors\EmailAlreadyExistsError.cs" />
<Compile Include="Domain\Errors\DatabaseEmptyError.cs" />
<Compile Include="Domain\Errors\InsuficientPrivilegesError.cs" />
<Compile Include="Domain\Errors\NameNotFound.cs" />
<Compile Include="Domain\Errors\IdNotFound.cs" />
<Compile Include="Domain\Errors\NameNotFoundError.cs" />
<Compile Include="Domain\Errors\IdNotFoundError.cs" />
<Compile Include="Domain\Errors\IncorrectPasswordError.cs" />
<Compile Include="Domain\Errors\IBaseError.cs" />
<Compile Include="Domain\Errors\InvalidEmailError.cs" />
<Compile Include="Domain\Errors\InvalidNameError.cs" />
<Compile Include="Domain\Errors\InvalidPassword.cs" />
<Compile Include="Domain\Errors\InvalidPasswordError.cs" />
<Compile Include="Domain\Errors\EmailNotFoundError.cs" />
<Compile Include="Domain\Errors\InvalidPhoneNumber.cs" />
<Compile Include="Domain\Errors\InvalidPhoneNumberError.cs" />
<Compile Include="Domain\Errors\NoError.cs" />
<Compile Include="Domain\Errors\DatabaseExceptionError.cs" />
<Compile Include="Domain\DTOs\UserDTO.cs" />
<Compile Include="Domain\Errors\NameAlreadyExists.cs" />
<Compile Include="Domain\Errors\NameAlreadyExistsError.cs" />
<Compile Include="Domain\Modules\Users\IUsersModule.cs" />
<Compile Include="Domain\Repositories\IUsersRepository.cs" />
<Compile Include="Domain\Usecases\Users\IGenerateReport.cs" />
Expand Down
28 changes: 28 additions & 0 deletions UsersDBApiTests/Infra/Database/Models/UserModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsersDBApi.Domain.DTOs;
using UsersDBApi.Infra.Database.Models;

namespace UsersDBApiTests.Infra.Database.Models
{
[TestClass]
public class UserModelTests
{
[TestMethod]
public void CreateMustReturnUserModel()
{
var user = new UserDTO("username", "[email protected]", "3799034578", "userpass", UserLevel.Administrator);

var result = UserModel.Create(user);

Assert.AreEqual(user.Name, result.Name);
Assert.AreEqual(user.Email, result.Email);
Assert.AreEqual(user.Phone, result.Phone);
Assert.AreEqual(user.Password, result.Password);
Assert.AreEqual(user.Level, result.Level);
}
}
}
22 changes: 0 additions & 22 deletions UsersDBApiTests/Infra/Repositories/UsersRepositoryTests.cs

This file was deleted.

67 changes: 59 additions & 8 deletions UsersDBApiTests/Infra/Usecases/CreateUserTests.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UsersDBApi.Infra.Usecases;
using UsersDBApi.Domain.DTOs;
using UsersDBApi.Domain.Errors;
using UsersDBApi.Infra.Usecases.Users;
using UsersDBApiTests.Mocks;

namespace UsersDBApiTests.Infra.Usecases
{
[TestClass]
public class CreateUserTests
{
[TestMethod]
public void MustReturnInvalidNameError()
{
// Arrange
var repository = new UsersRepositoryMock();
var usecase = new CreateUser(repository);
var result = usecase.Handle(MockedData.UserInvalidName);

Assert.IsTrue(result.IsLeft);
Assert.IsTrue(result.LeftOrDefault() is InvalidNameError);
}

[TestMethod]
public void MustReturnInvalidEmailError()
{
var repository = new UsersRepositoryMock();
var usecase = new CreateUser(repository);
var result = usecase.Handle(MockedData.UserInvalidEmail);

Assert.IsTrue(result.IsLeft);
Assert.IsTrue(result.LeftOrDefault() is InvalidEmailError);
}

[TestMethod]
public void MustReturnInvalidPhoneNumber()
{
var repository = new UsersRepositoryMock();
var usecase = new CreateUser(repository);
var result = usecase.Handle(MockedData.UserInvalidPhone);

Assert.IsTrue(result.IsLeft);
Assert.IsTrue(result.LeftOrDefault() is InvalidPhoneNumberError);
}

[TestMethod]
public void MustReturnInvalidPasswordError()
{
var repository = new UsersRepositoryMock();
var usecase = new CreateUser(repository);
var result = usecase.Handle(MockedData.UserInvalidPassword);

Assert.IsTrue(result.IsLeft);
Assert.IsTrue(result.LeftOrDefault() is InvalidPasswordError);
}

[TestMethod]
public void MustReturnCreatedUser()
{
var repository = new UsersRepositoryMock();
var usecase = new CreateUser(repository);
var result = usecase.Handle(MockedData.NewUser);

Assert.IsTrue(result.IsRight);
Assert.AreEqual(result.RightOrDefault().Name, MockedData.NewUser.Name);
Assert.AreEqual(result.RightOrDefault().Email, MockedData.NewUser.Email);
Assert.AreEqual(result.RightOrDefault().Phone, MockedData.NewUser.Phone);
Assert.AreEqual(result.RightOrDefault().Password, MockedData.NewUser.Password);
Assert.AreEqual(result.RightOrDefault().Level, MockedData.NewUser.Level);
}
}
}
36 changes: 36 additions & 0 deletions UsersDBApiTests/Infra/Usecases/GetAllUsersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsersDBApi.Infra.Usecases.Users;
using UsersDBApiTests.Mocks;

namespace UsersDBApiTests.Infra.Usecases
{
[TestClass]
public class GetAllUsersTests
{
[TestMethod]
public void MustReturnAllUsers()
{
var repository = new UsersRepositoryMock();
var usecase = new GetAllUsers(repository);
var result = usecase.Handle();
var users = result.RightOrDefault();

Assert.IsTrue(result.IsRight);

int i = 0;
foreach (var user in users)
{
Assert.AreEqual(MockedData.Users[i].Name, user.Name);
Assert.AreEqual(MockedData.Users[i].Email, user.Email);
Assert.AreEqual(MockedData.Users[i].Phone, user.Phone);
Assert.AreEqual(MockedData.Users[i].Password, user.Password);
Assert.AreEqual(MockedData.Users[i].Level, user.Level);
i++;
}
}
}
}
Loading

0 comments on commit 04c89e6

Please sign in to comment.