From af352c677d1803f0ee6d95da649fdc2993b1ba43 Mon Sep 17 00:00:00 2001 From: Mochika Date: Thu, 28 Nov 2024 00:26:08 +0100 Subject: [PATCH 1/5] ADD: Structure Project --- .gitignore | 369 ++++++++++++++++++ AppYevhenLetin/AppYevhenLetin.csproj | 19 + .../Controllers/RetailsController.cs | 21 + .../MarketParties/Entities/Retailer.cs | 6 + .../Services/Interfaces/IRetails.cs | 6 + .../MarketParties/Services/RetailsService.cs | 6 + AppYevhenLetin/Properties/launchSettings.json | 41 ++ 7 files changed, 468 insertions(+) create mode 100644 .gitignore create mode 100644 AppYevhenLetin/AppYevhenLetin.csproj create mode 100644 AppYevhenLetin/MarketParties/Controllers/RetailsController.cs create mode 100644 AppYevhenLetin/MarketParties/Entities/Retailer.cs create mode 100644 AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs create mode 100644 AppYevhenLetin/MarketParties/Services/RetailsService.cs create mode 100644 AppYevhenLetin/Properties/launchSettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d867f90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,369 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ + +/.vs +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd +/CursoCore/Properties +.vs/ +bin/ +.obj/ \ No newline at end of file diff --git a/AppYevhenLetin/AppYevhenLetin.csproj b/AppYevhenLetin/AppYevhenLetin.csproj new file mode 100644 index 0000000..1f89061 --- /dev/null +++ b/AppYevhenLetin/AppYevhenLetin.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + AppYevhenLetin + AppYevhenLetin + + + + + + + + + + + diff --git a/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs b/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs new file mode 100644 index 0000000..ddfa039 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.Mvc; + +namespace PruebaYevhenLetin.Controllers; + +[ApiController] +[Route("[controller]")] +public class RetailsController : ControllerBase +{ + private readonly ILogger _logger; + + public RetailsController() + { + + } + + [HttpGet(Name = "Get")] + public IEnumerable Get() + { + return; + } +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Entities/Retailer.cs b/AppYevhenLetin/MarketParties/Entities/Retailer.cs new file mode 100644 index 0000000..9b2bfc7 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Entities/Retailer.cs @@ -0,0 +1,6 @@ +namespace PruebaYevhenLetin.Controllers; + +public class Retailer +{ + +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs b/AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs new file mode 100644 index 0000000..38169a8 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs @@ -0,0 +1,6 @@ +namespace PruebaYevhenLetin.Controllers; + +public interface IRetails +{ + +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/RetailsService.cs b/AppYevhenLetin/MarketParties/Services/RetailsService.cs new file mode 100644 index 0000000..74af442 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/RetailsService.cs @@ -0,0 +1,6 @@ +namespace PruebaYevhenLetin.Controllers; + +public class RetailsService: IRetails +{ + +} \ No newline at end of file diff --git a/AppYevhenLetin/Properties/launchSettings.json b/AppYevhenLetin/Properties/launchSettings.json new file mode 100644 index 0000000..f66e259 --- /dev/null +++ b/AppYevhenLetin/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:40341", + "sslPort": 44339 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5034", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7040;http://localhost:5034", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} From 2c371b3dc2ef125a217f8739b9b2c17a1cf8cbc1 Mon Sep 17 00:00:00 2001 From: Mochika Date: Thu, 28 Nov 2024 04:40:00 +0100 Subject: [PATCH 2/5] ADD: AppYevhenLetin Project --- .gitignore | 3 +- AppYevhenLetin/AppYevhenLetin.csproj | 8 +-- AppYevhenLetin/Data/DbContext.cs | 13 +++++ .../Controllers/RetailsController.cs | 44 +++++++++++++-- .../MarketParties/Entities/Retailer.cs | 19 ++++++- .../MarketParties/Services/ApiService.cs | 55 +++++++++++++++++++ .../MarketParties/Services/DbSeeder.cs | 35 ++++++++++++ .../Services/Interfaces/IApiService.cs | 8 +++ .../Services/Interfaces/IRetailer.cs | 9 +++ .../Services/Interfaces/IRetails.cs | 6 -- .../MarketParties/Services/RetailerService.cs | 24 ++++++++ .../MarketParties/Services/RetailsService.cs | 6 -- AppYevhenLetin/Program.cs | 49 +++++++++++++++++ AppYevhenLetin/WebApplication1.http | 6 ++ AppYevhenLetin/appsettings.Development.json | 5 ++ 15 files changed, 265 insertions(+), 25 deletions(-) create mode 100644 AppYevhenLetin/Data/DbContext.cs create mode 100644 AppYevhenLetin/MarketParties/Services/ApiService.cs create mode 100644 AppYevhenLetin/MarketParties/Services/DbSeeder.cs create mode 100644 AppYevhenLetin/MarketParties/Services/Interfaces/IApiService.cs create mode 100644 AppYevhenLetin/MarketParties/Services/Interfaces/IRetailer.cs delete mode 100644 AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs create mode 100644 AppYevhenLetin/MarketParties/Services/RetailerService.cs delete mode 100644 AppYevhenLetin/MarketParties/Services/RetailsService.cs create mode 100644 AppYevhenLetin/Program.cs create mode 100644 AppYevhenLetin/WebApplication1.http create mode 100644 AppYevhenLetin/appsettings.Development.json diff --git a/.gitignore b/.gitignore index d867f90..7602942 100644 --- a/.gitignore +++ b/.gitignore @@ -366,4 +366,5 @@ FodyWeavers.xsd /CursoCore/Properties .vs/ bin/ -.obj/ \ No newline at end of file +.obj/ +.run/ \ No newline at end of file diff --git a/AppYevhenLetin/AppYevhenLetin.csproj b/AppYevhenLetin/AppYevhenLetin.csproj index 1f89061..fb1a10d 100644 --- a/AppYevhenLetin/AppYevhenLetin.csproj +++ b/AppYevhenLetin/AppYevhenLetin.csproj @@ -9,11 +9,9 @@ - - - - - + + + diff --git a/AppYevhenLetin/Data/DbContext.cs b/AppYevhenLetin/Data/DbContext.cs new file mode 100644 index 0000000..495de18 --- /dev/null +++ b/AppYevhenLetin/Data/DbContext.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using PruebaYevhenLetin.Controllers; +using PruebaYevhenLetin.Entity; + +namespace WebApplication1.Data; + +public class AppDbContext : DbContext +{ + public AppDbContext(DbContextOptions options) : base(options) { } + + public DbSet Retailers { get; set; } + +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs b/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs index ddfa039..03b4fda 100644 --- a/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs +++ b/AppYevhenLetin/MarketParties/Controllers/RetailsController.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using PruebaYevhenLetin.Entity; +using PruebaYevhenLetin.Services; namespace PruebaYevhenLetin.Controllers; @@ -6,16 +8,48 @@ namespace PruebaYevhenLetin.Controllers; [Route("[controller]")] public class RetailsController : ControllerBase { - private readonly ILogger _logger; + private readonly IRetailer _retailer; - public RetailsController() + public RetailsController(IRetailer retailerService) { + _retailer = retailerService; + } + [HttpGet] + public async Task>> GetAll() + { + try + { + var registros = await _retailer.GetAllRetailersAsync(); + return Ok(registros); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } } - [HttpGet(Name = "Get")] - public IEnumerable Get() + + [HttpGet("{primaryKey}")] + public async Task> GetByReCode(int primaryKey) { - return; + try + { + var registro = await _retailer.GetRetailerByCodeAsync(primaryKey); + + if (registro is null) + { + return NotFound(new { Message = $"No se encontro primary key {primaryKey}" }); + } + + return Ok(registro); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } } \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Entities/Retailer.cs b/AppYevhenLetin/MarketParties/Entities/Retailer.cs index 9b2bfc7..68a032b 100644 --- a/AppYevhenLetin/MarketParties/Entities/Retailer.cs +++ b/AppYevhenLetin/MarketParties/Entities/Retailer.cs @@ -1,6 +1,21 @@ -namespace PruebaYevhenLetin.Controllers; +using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; + +namespace PruebaYevhenLetin.Entity; public class Retailer { - + [Key] + [JsonPropertyName("id")] + public int Id { get; set; } + [JsonPropertyName("reCode")] + public string ReCode { get; set; } + [JsonPropertyName("reName")] + public string ReName { get; set; } + [JsonPropertyName("country")] + public string Country { get; set; } + [JsonPropertyName("codingScheme")] + public string CodingScheme { get; set; } + + } \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/ApiService.cs b/AppYevhenLetin/MarketParties/Services/ApiService.cs new file mode 100644 index 0000000..009b148 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/ApiService.cs @@ -0,0 +1,55 @@ +using System.Net; +using Newtonsoft.Json; +using PruebaYevhenLetin.Entity; + +namespace PruebaYevhenLetin.Services; + +public class ApiService: IApiService +{ + private readonly HttpClient _httpClient; + private readonly string _retailersApiUrl; + + public ApiService(HttpClient httpClient, IConfiguration config) + { + _httpClient = httpClient; + _retailersApiUrl = config["ApiSettings:RetailersApiUrl"] ?? string.Empty; + } + + public async Task> GetAllRetailersFromApiAsync() + { + try + { + // GET all retailers from api + var response = await _httpClient.GetAsync(_retailersApiUrl); + + switch (response.StatusCode) + { + case HttpStatusCode.OK: + var content = await response.Content.ReadAsStringAsync(); + var retailers = JsonConvert.DeserializeObject>(content); + return retailers ?? Enumerable.Empty(); + + case HttpStatusCode.NoContent: + Console.WriteLine("No content available (204). Returning empty list."); + return Enumerable.Empty(); + + case HttpStatusCode.BadRequest: + var errorContent = await response.Content.ReadAsStringAsync(); + Console.WriteLine($"Error 400: {errorContent}"); + return Enumerable.Empty(); + default: + var defaultErrorContent = await response.Content.ReadAsStringAsync(); + Console.WriteLine($"Error {response.StatusCode}: {defaultErrorContent}"); + return Enumerable.Empty(); + } + } + catch (Exception ex) + { + + Console.WriteLine($"Exception: {ex.Message}"); + return Enumerable.Empty(); + } + + } + +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/DbSeeder.cs b/AppYevhenLetin/MarketParties/Services/DbSeeder.cs new file mode 100644 index 0000000..20ac463 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/DbSeeder.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore; +using WebApplication1.Data; + +namespace PruebaYevhenLetin.Services; + +public class DbSeeder +{ + private readonly IApiService _apiService; + private readonly AppDbContext _context; + + public DbSeeder(IApiService apiService, AppDbContext data) + { + _apiService = apiService; + _context = data; + } + + // Metod insert data to daatabase from API + public async Task SeedDatabaseAsync() + { + var retailersFromApi = await _apiService.GetAllRetailersFromApiAsync(); + + foreach (var retailer in retailersFromApi) + { + var existingRetailer = await _context.Retailers + .FirstOrDefaultAsync(r => r.ReCode == retailer.ReCode); + + if (existingRetailer == null) + { + _context.Retailers.Add(retailer); + } + } + + await _context.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/Interfaces/IApiService.cs b/AppYevhenLetin/MarketParties/Services/Interfaces/IApiService.cs new file mode 100644 index 0000000..c07db33 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/Interfaces/IApiService.cs @@ -0,0 +1,8 @@ +using PruebaYevhenLetin.Entity; + +namespace PruebaYevhenLetin.Services; + +public interface IApiService +{ + Task> GetAllRetailersFromApiAsync(); +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/Interfaces/IRetailer.cs b/AppYevhenLetin/MarketParties/Services/Interfaces/IRetailer.cs new file mode 100644 index 0000000..e0a1578 --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/Interfaces/IRetailer.cs @@ -0,0 +1,9 @@ +using PruebaYevhenLetin.Entity; + +namespace PruebaYevhenLetin.Services; + +public interface IRetailer +{ + Task> GetAllRetailersAsync(); + Task GetRetailerByCodeAsync(int primaryKey); +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs b/AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs deleted file mode 100644 index 38169a8..0000000 --- a/AppYevhenLetin/MarketParties/Services/Interfaces/IRetails.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace PruebaYevhenLetin.Controllers; - -public interface IRetails -{ - -} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/RetailerService.cs b/AppYevhenLetin/MarketParties/Services/RetailerService.cs new file mode 100644 index 0000000..13c6d9c --- /dev/null +++ b/AppYevhenLetin/MarketParties/Services/RetailerService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using PruebaYevhenLetin.Entity; +using WebApplication1.Data; + +namespace PruebaYevhenLetin.Services; + +public class RetailerService(AppDbContext data) : IRetailer +{ + private IRetailer _retailerImplementation; + + public async Task> GetAllRetailersAsync() + { + return await data.Retailers.ToListAsync(); // Usar 'db' para acceder a los datos + } + + + + public async Task GetRetailerByCodeAsync(int primaryKey) + { + return await data.Retailers.FirstOrDefaultAsync(r => r.Id == primaryKey); + } + + +} \ No newline at end of file diff --git a/AppYevhenLetin/MarketParties/Services/RetailsService.cs b/AppYevhenLetin/MarketParties/Services/RetailsService.cs deleted file mode 100644 index 74af442..0000000 --- a/AppYevhenLetin/MarketParties/Services/RetailsService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace PruebaYevhenLetin.Controllers; - -public class RetailsService: IRetails -{ - -} \ No newline at end of file diff --git a/AppYevhenLetin/Program.cs b/AppYevhenLetin/Program.cs new file mode 100644 index 0000000..b78426f --- /dev/null +++ b/AppYevhenLetin/Program.cs @@ -0,0 +1,49 @@ +using Microsoft.EntityFrameworkCore; +using PruebaYevhenLetin.Controllers; +using PruebaYevhenLetin.Services; +using WebApplication1.Data; + +namespace WebApplication1; + +public class Program +{ + public static async Task Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + builder.Services.AddDbContext(options => + options.UseInMemoryDatabase("InMemoryDb")); + builder.Services.AddHttpClient(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + using (var scope = app.Services.CreateScope()) + { + var seeder = scope.ServiceProvider.GetRequiredService(); + await seeder.SeedDatabaseAsync(); + } + + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } +} \ No newline at end of file diff --git a/AppYevhenLetin/WebApplication1.http b/AppYevhenLetin/WebApplication1.http new file mode 100644 index 0000000..83ba4bc --- /dev/null +++ b/AppYevhenLetin/WebApplication1.http @@ -0,0 +1,6 @@ +@WebApplication1_HostAddress = http://localhost:5034 + +GET {{WebApplication1_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/AppYevhenLetin/appsettings.Development.json b/AppYevhenLetin/appsettings.Development.json new file mode 100644 index 0000000..fbb72ee --- /dev/null +++ b/AppYevhenLetin/appsettings.Development.json @@ -0,0 +1,5 @@ +{ + "ApiSettings": { + "RetailersApiUrl": "https://api.opendata.esett.com/EXP01/Retailers" + } +} From 748a2428c59dcdfcc6ee097262f47cabf78e2142 Mon Sep 17 00:00:00 2001 From: Mochika Date: Thu, 28 Nov 2024 04:41:44 +0100 Subject: [PATCH 3/5] ADD: Test Project --- TestApp/RetailsTest.cs | 101 +++++++++++++++++++++++++++++++++++++++++ TestApp/TestApp.csproj | 28 ++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 TestApp/RetailsTest.cs create mode 100644 TestApp/TestApp.csproj diff --git a/TestApp/RetailsTest.cs b/TestApp/RetailsTest.cs new file mode 100644 index 0000000..93e53a7 --- /dev/null +++ b/TestApp/RetailsTest.cs @@ -0,0 +1,101 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using PruebaYevhenLetin; +using PruebaYevhenLetin.Entity; +using System.Net; +using System.Net.Http; +using System.Text.Json; +using System.Threading.Tasks; +using WebApplication1; +using WebApplication1.Data; +using Xunit; + +public class RetailsControllerTests : IClassFixture> +{ + private readonly HttpClient _client; + private readonly WebApplicationFactory _factory; + + public RetailsControllerTests(WebApplicationFactory factory) + { + _factory = factory; + _client = _factory.CreateClient(); // Crear un cliente HTTP para la aplicación + } + + [Fact] + public async Task GetAll_ReturnsOkResponse_IsSuccess() + { + // Act + var response = await _client.GetAsync("/retails"); + + // Assert: 200 OK + Assert.NotNull(response); + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + } + + // Test get all retailers + [Fact] + public async Task GetAll_ReturnsOkResponse_WithData() + { + //Arrange + using (var scope = _factory.Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + context.Retailers.Add(new Retailer { ReCode = "1234567890123", ReName = "Aalto Energia", Country = "FI", CodingScheme = "GS1" }); + context.Retailers.Add(new Retailer { ReCode = "9876543210987", ReName = "Example Corp", Country = "US", CodingScheme = "GS1" }); + context.SaveChanges(); + } + + // Act: + var response = await _client.GetAsync("/retails"); + var content = await response.Content.ReadAsStringAsync(); + var retailers = JsonSerializer.Deserialize>(content); + + // Assert: retails hasn't empty + Assert.NotEmpty(retailers); + } + + // Test get a retails with primaryKey + [Fact] + public async Task GetByReCode_ReturnsOkResponse_WhenExists() + { + //Arrange + using (var scope = _factory.Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + context.Retailers.Add(new Retailer + { + Id = 10000, + ReCode = "3456789012345", + ReName = "Retailer Three", + Country = "FR", + CodingScheme = "UPC" + }); + context.SaveChanges(); + } + + // Act + var response = await _client.GetAsync("/retails/10000"); + response.EnsureSuccessStatusCode(); + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + var json = await response.Content.ReadAsStringAsync(); + var retailer = JsonSerializer.Deserialize(json, options); + + //Assert: ReCode has same number + Assert.Equal("3456789012345", retailer.ReCode); + } + + // Test get retailer with primary key don't exist + [Fact] + public async Task GetByReCode_ReturnsNotFound_WhenNotExists() + { + // Act + var response = await _client.GetAsync("/retails/10000"); + + // Assert: 404 Not Found + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } +} diff --git a/TestApp/TestApp.csproj b/TestApp/TestApp.csproj new file mode 100644 index 0000000..ad4fb59 --- /dev/null +++ b/TestApp/TestApp.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + From 325fef23d7f72a0224666dfc77d17894e87bb30b Mon Sep 17 00:00:00 2001 From: Mochika Date: Thu, 28 Nov 2024 04:42:21 +0100 Subject: [PATCH 4/5] ADD: Other Files --- .idea/.gitignore | 13 ++++++++ .idea/aws.xml | 11 +++++++ .idea/indexLayout.xml | 8 +++++ .idea/vcs.xml | 6 ++++ .run/Multi-Launch_ prueba_tecnica_net.run.xml | 32 +++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/aws.xml create mode 100644 .idea/indexLayout.xml create mode 100644 .idea/vcs.xml create mode 100644 .run/Multi-Launch_ prueba_tecnica_net.run.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..d9350e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/modules.xml +/contentModel.xml +/.idea.prueba_tecnica_net.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/aws.xml b/.idea/aws.xml new file mode 100644 index 0000000..b63b642 --- /dev/null +++ b/.idea/aws.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/.idea/indexLayout.xml b/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.run/Multi-Launch_ prueba_tecnica_net.run.xml b/.run/Multi-Launch_ prueba_tecnica_net.run.xml new file mode 100644 index 0000000..9993d62 --- /dev/null +++ b/.run/Multi-Launch_ prueba_tecnica_net.run.xml @@ -0,0 +1,32 @@ + + + + \ No newline at end of file From a11e699af0ef7614ff6ddf796792cb5ae84b7f90 Mon Sep 17 00:00:00 2001 From: Mochika Date: Thu, 28 Nov 2024 04:52:34 +0100 Subject: [PATCH 5/5] ADD: Comments --- .../MarketParties/Services/RetailerService.cs | 2 +- AppYevhenLetin/Program.cs | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/AppYevhenLetin/MarketParties/Services/RetailerService.cs b/AppYevhenLetin/MarketParties/Services/RetailerService.cs index 13c6d9c..7c16145 100644 --- a/AppYevhenLetin/MarketParties/Services/RetailerService.cs +++ b/AppYevhenLetin/MarketParties/Services/RetailerService.cs @@ -10,7 +10,7 @@ public class RetailerService(AppDbContext data) : IRetailer public async Task> GetAllRetailersAsync() { - return await data.Retailers.ToListAsync(); // Usar 'db' para acceder a los datos + return await data.Retailers.ToListAsync(); } diff --git a/AppYevhenLetin/Program.cs b/AppYevhenLetin/Program.cs index b78426f..42fe239 100644 --- a/AppYevhenLetin/Program.cs +++ b/AppYevhenLetin/Program.cs @@ -10,38 +10,48 @@ public class Program public static async Task Main(string[] args) { var builder = WebApplication.CreateBuilder(args); - + + //Database in memory builder.Services.AddDbContext(options => options.UseInMemoryDatabase("InMemoryDb")); + + //Services builder.Services.AddHttpClient(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + + // Swagger builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); - + + //Create DB using (var scope = app.Services.CreateScope()) { var seeder = scope.ServiceProvider.GetRequiredService(); await seeder.SeedDatabaseAsync(); } - // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"); //JSON + options.DocumentTitle = "Adevinta"; // Title + options.DisplayRequestDuration(); // time request + + }); } + //Middleware app.UseHttpsRedirection(); app.UseAuthorization(); - app.MapControllers(); app.Run();