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 not shown.
Binary file added ApiRestfullpic/.vs/ApiRestfullpic/v15/.suo
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
991 changes: 991 additions & 0 deletions ApiRestfullpic/.vs/config/applicationhost.config

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions ApiRestfullpic/ApiRestfullpic.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiRestfullpic", "ApiRestfullpic\ApiRestfullpic.csproj", "{D8D0AA85-B6FC-4D4B-ACE6-13A2C8CDF68D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D8D0AA85-B6FC-4D4B-ACE6-13A2C8CDF68D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8D0AA85-B6FC-4D4B-ACE6-13A2C8CDF68D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8D0AA85-B6FC-4D4B-ACE6-13A2C8CDF68D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8D0AA85-B6FC-4D4B-ACE6-13A2C8CDF68D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9672EA7F-517B-445D-8F72-0999EB496C8F}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/ApiRestfullpic.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
<PackageReference Include="Tapioca.HATEOAS" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>


</Project>
15 changes: 15 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Business/IUserBusiness.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ApiRestfullpic.Model;
using System.Collections.Generic;
using Tapioca.HATEOAS.Utils;

namespace ApiRestfullpic.Business
{
public interface IUserBusiness
{
T FindById(long id);
T FindByGuid(string guid);
List<T> FindByKeyWord(string keyword);
PagedSearchDTO<T> FindByKeyWordPaged(string keyword, string sortDirection, int pageSize, int page);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using ApiRestfullpic.Model;
using ApiRestfullpic.Repository;
using Tapioca.HATEOAS.Utils;

namespace ApiRestfullpic.Business.Implemantations
{
public class UserBusinessImpl : IUserBusiness
{
protected IUserRepository _repository;

public UserBusinessImpl(IUserRepository repository)
{
_repository = repository;

}

public T FindById(long id)
{
return _repository.FindById(id);
}

public T FindByGuid(string guid)
{
return _repository.FindByGuid(guid);

}

public List<T> FindByKeyWord(string keyword)
{
return _repository.FindByKeyWord(keyword);
}

public PagedSearchDTO<T> FindByKeyWordPaged(string keyword, string sortDirection, int pageSize, int page)
{
page = page > 0 ? page - 1 : 0;
int offset = pageSize * page;
string query = @"select * from Users u where 1 = 1 ";
if (!string.IsNullOrEmpty(keyword)) query = query + $" and u.nome like '%{keyword}%' && u.userName like '%{keyword}%'";

query = query + $" order by field(guid,(select id from order1 where id = u.guid),(select id from order2 where id = u.guid)) {sortDirection} limit {pageSize} offset {offset}";

string countQuery = @"select count(*) from Users u where 1 = 1 ";
if (!string.IsNullOrEmpty(keyword)) countQuery = countQuery + $" and u.nome like '%{keyword}%' && u.userName like '%{keyword}%'";

var users = _repository.FindByKeyWordPaged(query);

int totalResults = _repository.GetCount(countQuery);

return new PagedSearchDTO<T>
{
CurrentPage = page + 1,
List = users,
PageSize = pageSize,
SortDirections = sortDirection,
TotalResults = totalResults
};

}
}
}
70 changes: 70 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using ApiRestfullpic.Business;
using ApiRestfullpic.Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace ApiRestfullpic.Controllers
{
[ApiVersion("1")]
[Route("api/[controller]/v{version:apiVersion}")]
[ApiController]
public class UsersController : ControllerBase
{

private IUserBusiness _userBusiness;

public UsersController(IUserBusiness userBusiness)
{
_userBusiness = userBusiness;
}
#region HTTP GET

/// // GET api/users/version/{id}
[ProducesResponseType((200), Type = typeof(List<T>))]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
[HttpGet, Route("id/{id}")]
public ActionResult FindById(long id)
{
var user = _userBusiness.FindById(id);
if (user == null) return NotFound();
return Ok(user);
}

/// GET api/users/version/{guid}
[ProducesResponseType((200), Type = typeof(List<T>))]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
[HttpGet, Route("guid/{guid}")]
public ActionResult FindByGuid(string guid)
{
var user = _userBusiness.FindByGuid(guid);
if (user == null) return NotFound();
return Ok(user);
}

/// GET api/users/desc/{pageSize}/{page}?={keyword}
///
///<remarks>
///Exemplo de requisição:
///https://localhost:44358/api/Users/v1/find-by-paged-search/desc/15/1?keyword=rafael
///</remarks>
[ProducesResponseType((200), Type = typeof(List<T>))]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
[ProducesResponseType(404)]
[HttpGet, Route("find-by-paged-search/{sortDirection}/{pageSize}/{page}")]
public IActionResult GetPagedSearch([FromQuery] string keyword, string sortDirection, int pageSize, int page)
{

return new OkObjectResult(_userBusiness.FindByKeyWordPaged(keyword, sortDirection, pageSize, page));
}


#endregion

}
}
20 changes: 20 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Model/Context/MySQLContext.cs
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.Threading.Tasks;

namespace ApiRestfullpic.Model.Context
{
public class MySQLContext : DbContext
{
public MySQLContext()
{

}

public MySQLContext(DbContextOptions<MySQLContext> options) : base(options) { }

public DbSet<T> Users { get; set; }
}
}
24 changes: 24 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Model/Users.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace ApiRestfullpic.Model
{
[DataContract]
public class T
{
[DataMember (Order = 1)]
public long id { get; set; }

[DataMember(Order = 2)]
public string guid { get; set; }

[DataMember(Order = 4)]
public string nome { get; set; }

[DataMember(Order = 3)]
public string userName { get; set; }
}
}
24 changes: 24 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace ApiRestfullpic
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
28 changes: 28 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49943",
"sslPort": 44358
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ApiRestfullpic": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
14 changes: 14 additions & 0 deletions ApiRestfullpic/ApiRestfullpic/Repository/IUserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ApiRestfullpic.Model;
using System.Collections.Generic;

namespace ApiRestfullpic.Repository
{
public interface IUserRepository
{
T FindById(long id);
T FindByGuid(string guid);
List<T> FindByKeyWord(string keyword);
List<T> FindByKeyWordPaged(string query);
int GetCount(string query);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ApiRestfullpic.Model;
using ApiRestfullpic.Model.Context;
using Microsoft.EntityFrameworkCore;

namespace ApiRestfullpic.Repository.Implemantations
{
public class UserRepositoryImpl : IUserRepository
{
private MySQLContext _context;
private DbSet<T> dataset;

public UserRepositoryImpl(MySQLContext context)
{
_context = context;
dataset = _context.Set<T>();

}

public T FindById(long id)
{
return _context.Users.SingleOrDefault(u => u.id.Equals(id));
}

public T FindByGuid(string guid)
{
return _context.Users.SingleOrDefault(u => u.guid.Equals(guid));
}

public List<T> FindByKeyWord(string keyword)
{
if (!string.IsNullOrWhiteSpace(keyword))
return _context.Users.Where(n => n.nome.Contains(keyword) && n.userName.Contains(keyword)).ToList();
else
return null;
}

public List<T> FindByKeyWordPaged(string query)
{
return dataset.FromSql<T>(query).ToList();

}

public int GetCount(string query)
{
var result = "";
using (var connection = _context.Database.GetDbConnection())
{
connection.Open();

using (var command = connection.CreateCommand())
{
command.CommandText = query;
result = command.ExecuteScalar().ToString();
}
}

return Int32.Parse(result);
}
}
}

Loading