Добавьте файлы проекта.
This commit is contained in:
90
Controllers/ContactController.cs
Normal file
90
Controllers/ContactController.cs
Normal 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"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user