diff --git a/Vetclinic/ClientApp/APIClient.cs b/Vetclinic/ClientApp/APIClient.cs new file mode 100644 index 0000000..0c23d1e --- /dev/null +++ b/Vetclinic/ClientApp/APIClient.cs @@ -0,0 +1,44 @@ +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; +using VetclinicContracts.ViewModels; + +namespace ClientApp +{ + public static class APIClient + { + private static readonly HttpClient _client = new(); + public static ClientViewModel? Client { get; set; } = null; + public static void Connect(IConfiguration configuration) + { + _client.BaseAddress = new Uri(configuration["IPAddress"]); + _client.DefaultRequestHeaders.Accept.Clear(); + _client.DefaultRequestHeaders.Accept.Add(new + MediaTypeWithQualityHeaderValue("application/json")); + } + public static T? GetRequest(string requestUrl) + { + var response = _client.GetAsync(requestUrl); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (response.Result.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(result); + } + else + { + throw new Exception(result); + } + } + public static void PostRequest(string requestUrl, T model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + var response = _client.PostAsync(requestUrl, data); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (!response.Result.IsSuccessStatusCode) + { + throw new Exception(result); + } + } + } +} diff --git a/Vetclinic/ClientApp/ClientApp.csproj b/Vetclinic/ClientApp/ClientApp.csproj index 52867a1..cdb17c7 100644 --- a/Vetclinic/ClientApp/ClientApp.csproj +++ b/Vetclinic/ClientApp/ClientApp.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/Vetclinic/ClientApp/Program.cs b/Vetclinic/ClientApp/Program.cs index 559dd3a..cf88e28 100644 --- a/Vetclinic/ClientApp/Program.cs +++ b/Vetclinic/ClientApp/Program.cs @@ -1,10 +1,12 @@ +using ClientApp; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); - +APIClient.Connect(builder.Configuration); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/Vetclinic/ClientApp/appsettings.json b/Vetclinic/ClientApp/appsettings.json index 10f68b8..c27c8d0 100644 --- a/Vetclinic/ClientApp/appsettings.json +++ b/Vetclinic/ClientApp/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "IPAddress": "http://localhost:5244" } diff --git a/Vetclinic/VetclinicDatabaseImplement/Implements/AnimalStorage.cs b/Vetclinic/VetclinicDatabaseImplement/Implements/AnimalStorage.cs index 1df627a..8c09360 100644 --- a/Vetclinic/VetclinicDatabaseImplement/Implements/AnimalStorage.cs +++ b/Vetclinic/VetclinicDatabaseImplement/Implements/AnimalStorage.cs @@ -21,14 +21,12 @@ namespace VetclinicDatabaseImplement.Implements } public List GetFilteredList(AnimalSearchModel model) { - if (string.IsNullOrEmpty(model.AnimalName)) - { - return new(); - } - using var context = new VetclinicDatabase(); - return context.Animals.Where(x => x.AnimalName.Contains(model.AnimalName)). - Select(x => x.GetViewModel).ToList(); - } + using var context = new VetclinicDatabase(); + return context.Animals.Where(x => x.ClientId == model.ClientId) + .Where(x => String.IsNullOrEmpty(model.AnimalName) || x.AnimalName.Contains(model.AnimalName)) + .Select(x => x.GetViewModel) + .ToList(); + } public AnimalViewModel? GetElement(AnimalSearchModel model) { if (string.IsNullOrEmpty(model.AnimalName) && !model.Id.HasValue) diff --git a/Vetclinic/VetclinicRestApi/Controllers/AnimalController.cs b/Vetclinic/VetclinicRestApi/Controllers/AnimalController.cs new file mode 100644 index 0000000..cdf5456 --- /dev/null +++ b/Vetclinic/VetclinicRestApi/Controllers/AnimalController.cs @@ -0,0 +1,93 @@ +using Microsoft.AspNetCore.Mvc; +using VetclinicContracts.BindingModels; +using VetclinicContracts.BusinessLogicsContracts; +using VetclinicContracts.SearchModels; +using VetclinicContracts.ViewModels; + +namespace VetclinicRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class AnimalController : Controller + { + private readonly ILogger _logger; + private readonly IAnimalLogic _animal; + public AnimalController(ILogger logger, IAnimalLogic animal) + { + _logger = logger; + _animal = animal; + } + [HttpGet] + public List GetAnimals(int clentId) + { + try + { + return _animal.ReadList(new AnimalSearchModel { ClientId = clentId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка животных"); + throw; + } + } + [HttpGet] + public Tuple? GetAnimal(int animalId) + { + try + { + var elem = _animal.ReadElement(new AnimalSearchModel { Id = animalId }); + if (elem == null) + { + return null; + } + return Tuple.Create(elem); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения животного по id={Id}", animalId); + throw; + } + } + [HttpPost] + public bool CreateAnimal(AnimalBindingModel model) + { + try + { + return _animal.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Не удалось создать животного"); + throw; + } + } + + [HttpPost] + public bool UpdateAnimal(AnimalBindingModel model) + { + try + { + return _animal.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Не удалось обновить животное"); + throw; + } + } + + [HttpPost] + public bool DeleteAnimal(AnimalBindingModel model) + { + try + { + return _animal.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления животного"); + throw; + } + } + } +} diff --git a/Vetclinic/VetclinicRestApi/Controllers/ClientController.cs b/Vetclinic/VetclinicRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..bfcfd93 --- /dev/null +++ b/Vetclinic/VetclinicRestApi/Controllers/ClientController.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Mvc; +using VetclinicContracts.BindingModels; +using VetclinicContracts.BusinessLogicsContracts; +using VetclinicContracts.SearchModels; +using VetclinicContracts.ViewModels; + +namespace VetclinicRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + public ClientController(ILogger logger, IClientLogic logic) + { + _logger = logger; + _logic = logic; + } + [HttpGet] + public ClientViewModel? Login(string email, string password) + { + try + { + return _logic.ReadElement(new ClientSearchModel + { + Email = email, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + [HttpPost] + public void Register(ClientBindingModel model) + { + try + { + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + [HttpPost] + public void UpdateData(ClientBindingModel model) + { + try + { + _logic.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + } +} diff --git a/Vetclinic/VetclinicRestApi/Controllers/DrugPurchaseController.cs b/Vetclinic/VetclinicRestApi/Controllers/DrugPurchaseController.cs new file mode 100644 index 0000000..f686c0e --- /dev/null +++ b/Vetclinic/VetclinicRestApi/Controllers/DrugPurchaseController.cs @@ -0,0 +1,66 @@ +using Microsoft.AspNetCore.Mvc; +using VetclinicContracts.BindingModels; +using VetclinicContracts.BusinessLogicsContracts; +using VetclinicContracts.SearchModels; +using VetclinicContracts.ViewModels; +using VetclinicDatabaseImplement.Models; + +namespace VetclinicRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class DrugPurchaseController : Controller + { + private readonly ILogger _logger; + private readonly IDrugPurchaseLogic _drugPurchase; + public DrugPurchaseController(ILogger logger, IDrugPurchaseLogic drugPurchase) + { + _logger = logger; + _drugPurchase = drugPurchase; + } + [HttpGet] + public Tuple? GetDrugPurchase(int drugPurchaseId) + { + try + { + var elem = _drugPurchase.ReadElement(new DrugPurchaseSearchModel { Id = drugPurchaseId }); + if (elem == null) + { + return null; + } + return Tuple.Create(elem); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения животного по id={Id}", drugPurchaseId); + throw; + } + } + [HttpGet] + public List? GetPurchases(int? clientId) + { + try + { + return _drugPurchase.ReadList(new DrugPurchaseSearchModel { ClientId = clientId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка покупок"); + throw; + } + } + [HttpPost] + public bool CreatePurchase(DrugPurchaseBindingModel model) + { + try + { + return _drugPurchase.CreateDrugPurchase(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Не удалось создать покупку"); + throw; + } + } + } +} diff --git a/Vetclinic/VetclinicRestApi/Controllers/WeatherForecastController.cs b/Vetclinic/VetclinicRestApi/Controllers/WeatherForecastController.cs deleted file mode 100644 index aa83b96..0000000 --- a/Vetclinic/VetclinicRestApi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace VetclinicRestApi.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/Vetclinic/VetclinicRestApi/Program.cs b/Vetclinic/VetclinicRestApi/Program.cs index 42078cc..ffbfdd3 100644 --- a/Vetclinic/VetclinicRestApi/Program.cs +++ b/Vetclinic/VetclinicRestApi/Program.cs @@ -1,12 +1,46 @@ +using Microsoft.OpenApi.Models; +using VetclinicBusinessLogic.BusinessLogics; +using VetclinicContracts.BusinessLogicsContracts; +using VetclinicContracts.StoragesContracts; +using VetclinicDatabaseImplement.Implements; + var builder = WebApplication.CreateBuilder(args); -builder.Logging.SetMinimumLevel(LogLevel.Trace); -builder.Logging.AddLog4Net("log4net.config"); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddTransient(); +//builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen( c => +{ + c.SwaggerDoc("v1", new OpenApiInfo { Title = "VetclinicRestApi", Version = "v1" }); +}); var app = builder.Build(); diff --git a/Vetclinic/VetclinicRestApi/WeatherForecast.cs b/Vetclinic/VetclinicRestApi/WeatherForecast.cs deleted file mode 100644 index 39149a3..0000000 --- a/Vetclinic/VetclinicRestApi/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace VetclinicRestApi -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file