Добавьте файлы проекта.

This commit is contained in:
zcher
2025-12-14 18:15:23 +03:00
parent 9d11002bed
commit 81524b0ffb
11 changed files with 290 additions and 0 deletions

14
ContactsApp.csproj Normal file
View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.0.1" />
</ItemGroup>
</Project>

6
ContactsApp.http Normal file
View File

@@ -0,0 +1,6 @@
@ContactsApp_HostAddress = http://localhost:5246
GET {{ContactsApp_HostAddress}}/weatherforecast/
Accept: application/json
###

3
ContactsApp.slnx Normal file
View File

@@ -0,0 +1,3 @@
<Solution>
<Project Path="ContactsApp.csproj" />
</Solution>

View File

@@ -0,0 +1,90 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Text.Json;
namespace ContactsApp
{
[ApiController]
[Route("api/contacts")]
public class ContactController(IContactRepository repository) : ControllerBase
{
[HttpGet]
public ActionResult<IEnumerable<Contact>> GetAll()
{
var contacts = repository.GetAll().ToList();
return Ok(contacts);
}
[HttpPost]
public ActionResult<Contact> Create([FromBody] Contact contact)
{
repository.AddContact(contact);
return Ok(contact);
}
[HttpGet("{id:Guid}")]
public ActionResult<Contact> GetById(Guid id)
{
var contact = repository.GetContactById(id);
if (contact == null)
return NotFound();
return Ok(contact);
}
[HttpPost("{id:Guid}")]
public ActionResult Update(Guid id, [FromBody] Contact contact)
{
repository.UpdateContact(id, contact);
return Ok();
}
[HttpDelete("{id:Guid}")]
public ActionResult Delete(Guid id)
{
var contact = repository.GetContactById(id);
if (contact == null)
return NotFound();
repository.DeleteContact(id);
return Ok();
}
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("<22><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.");
using var stream = new MemoryStream();
await file.CopyToAsync(stream);
string json = System.Text.Encoding.UTF8.GetString(stream.ToArray());
try
{
repository.LoadJSON(json); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> JSON <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
catch (Exception ex)
{
return BadRequest("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> JSON: " + ex.Message);
}
return Ok("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.");
}
[HttpGet("download")]
public IActionResult Download()
{
string json = repository.SaveJSON(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> JSON <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);
return File(
bytes,
"application/json",
"contacts.json"
);
}
}
}

16
IContactRepository.cs Normal file
View File

@@ -0,0 +1,16 @@
using System;
using System.Threading.Tasks;
namespace ContactsApp
{
public interface IContactRepository
{
public void AddContact(Contact contact);
public void UpdateContact(Guid id, Contact contact);
public void DeleteContact(Guid id);
public Contact GetContactById(Guid id);
public IEnumerable<Contact> GetAll();
public void LoadJSON(string file);
public string SaveJSON();
}
}

33
Models/Contact.cs Normal file
View File

@@ -0,0 +1,33 @@
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
namespace ContactsApp
{
public class Contact
{
public Guid Id { get; }
public string Name { get; set; }
public string? Email { get; set; }
public string Phone { get; set; }
public Contact(string name, string? email, string phone)
{
Id = Guid.NewGuid();
Name = name;
try
{
var addr = new MailAddress(email);
Email = email;
}
catch (Exception ex)
{
throw new Exception("Invalid email");
}
Phone = phone;
}
}
}

32
Program.cs Normal file
View File

@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Swagger;
namespace ContactsApp;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddAuthorization();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IContactRepository, ContactRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -> <20><><EFBFBD><EFBFBD><EFBFBD> -> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> -> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -> Kestrel -> (HttpContext) Middlewares -> Controller (mapping) -> AfterMethod -> Method -> BeforeMethod
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI();
app.MapControllers();
app.Run();
}
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5043",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,64 @@
using Microsoft.Win32;
using System;
using System.Runtime;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ContactsApp
{
public class ContactRepository : IContactRepository
{
List<Contact> Contacts = new ();
public void AddContact(Contact contact)
{
CheckUnique(contact);
Contacts.Add(contact);
}
public void UpdateContact(Guid id, Contact contact)
{
CheckUnique(contact);
Contact previous = GetContactById(id);
if (previous == null)
{
Contacts.Add(contact);
return;
}
previous.Name = contact.Name;
previous.Email = contact.Email;
previous.Phone = contact.Phone;
}
public void DeleteContact(Guid id)
{
var contact = GetContactById(id);
if (contact == null)
return;
Contacts.Remove(contact);
}
public Contact GetContactById(Guid id)
{
return Contacts.Find(x => x.Id == id);
}
public void LoadJSON(string json)
{
var list = JsonSerializer.Deserialize<List<Contact>>(json);
Contacts.AddRange(list);
}
public string SaveJSON()
{
return JsonSerializer.Serialize(Contacts, new JsonSerializerOptions { WriteIndented = true });
}
public IEnumerable<Contact> GetAll() => Contacts;
private void CheckUnique(Contact contact)
{
if (Contacts.FirstOrDefault(x => x.Equals(contact)) != null)
throw new Exception("Такой контакт уже есть");
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

9
appsettings.json Normal file
View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}