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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,5 @@ ASALocalRun/
.localhistory/

# BeatPulse healthcheck temp database
healthchecksdb
healthchecksdb
/serilogsettings.Staging.json
60 changes: 39 additions & 21 deletions Controllers/TodoItemsController.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.Models;

using TodoApiDTO.IToDoServices;
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TodoItemsController : ControllerBase
{
private readonly TodoContext _context;

public TodoItemsController(TodoContext context)
private IToDoService _toDoService;
private readonly IMapper _mapper;
protected readonly ILogger _logger;
public TodoItemsController(IToDoService toDoService, IMapper mapper, ILogger<TodoItemsController> logger)
{
_context = context;
_toDoService = toDoService;
_mapper = mapper;
_logger = logger;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
{
return await _context.TodoItems
.Select(x => ItemToDTO(x))
.ToListAsync();
_logger.LogInformation("Hello From logging");
var result = _toDoService._todoRepository.FindAll().ToList();
return _mapper.Map<List<TodoItemDTO>>(result);
}

[HttpGet("{id}")]
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);
var todoItem = _toDoService._todoRepository.FindByCondition(x => x.Id == id).First();

if (todoItem == null)
{
return NotFound();
_logger.LogError("�� ������� ������ �����");
}

return ItemToDTO(todoItem);
return _mapper.Map<TodoItemDTO>(todoItem);
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDTO todoItemDTO)
{
if (id != todoItemDTO.Id)
{
_logger.LogError("���� �� ���������");
return BadRequest();
}

var todoItem = await _context.TodoItems.FindAsync(id);
var todoItem = _toDoService._todoRepository.FindByCondition(x => x.Id == id).First();
if (todoItem == null)
{
_logger.LogError("������ �� �������");
return NotFound();
}

Expand All @@ -58,10 +65,13 @@ public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDTO todoItemDTO

try
{
await _context.SaveChangesAsync();
_toDoService._todoRepository.Update(todoItem);
_toDoService.Save();

}
catch (DbUpdateConcurrencyException) when (!TodoItemExists(id))
{
_logger.LogError("������ �� �������");
return NotFound();
}

Expand All @@ -77,8 +87,8 @@ public async Task<ActionResult<TodoItemDTO>> CreateTodoItem(TodoItemDTO todoItem
Name = todoItemDTO.Name
};

_context.TodoItems.Add(todoItem);
await _context.SaveChangesAsync();
_toDoService._todoRepository.Create(todoItem);
_toDoService.Save();

return CreatedAtAction(
nameof(GetTodoItem),
Expand All @@ -89,22 +99,30 @@ public async Task<ActionResult<TodoItemDTO>> CreateTodoItem(TodoItemDTO todoItem
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTodoItem(long id)
{
var todoItem = await _context.TodoItems.FindAsync(id);
var todoItem = _toDoService._todoRepository.FindByCondition(x => x.Id == id).First();

if (todoItem == null)
{
_logger.LogError("������ �� �������");
return NotFound();
}

_context.TodoItems.Remove(todoItem);
await _context.SaveChangesAsync();
_toDoService._todoRepository.Delete(todoItem);
_toDoService.Save();

return NoContent();
}

private bool TodoItemExists(long id) =>
_context.TodoItems.Any(e => e.Id == id);

private bool TodoItemExists(long id)
{
var result = _toDoService._todoRepository.FindByCondition(x => x.Id == id).First();

if(result != null)
{
return true;
}
return false;
}
private static TodoItemDTO ItemToDTO(TodoItem todoItem) =>
new TodoItemDTO
{
Expand Down
10 changes: 10 additions & 0 deletions IToDoServices/IToDoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using TodoApiDTO.Repositories.Entities;

namespace TodoApiDTO.IToDoServices
{
public interface IToDoService
{
IToDoRepository _todoRepository { get; }
void Save();
}
}
31 changes: 31 additions & 0 deletions IToDoServices/ToDoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using TodoApi.Models;
using TodoApiDTO.Repositories.Entities;
using TodoApiDTO.Repositories.ToDo;

namespace TodoApiDTO.IToDoServices
{
public class ToDoService : IToDoService
{
private TodoContext _todoContext;
private IToDoRepository _toDoRepository;

public IToDoRepository _todoRepository
{
get
{
if (_toDoRepository == null) { _toDoRepository = new ToDoRepository(_todoContext); }
return _toDoRepository;
}
}

public ToDoService(TodoContext todoContext)
{
_todoContext = todoContext;
}

public void Save()
{
_todoContext.SaveChanges();
}
}
}
14 changes: 14 additions & 0 deletions Mapping/ToDoMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AutoMapper;
using TodoApi.Models;

namespace TodoApiDTO.Mapping
{
public class ToDoMappingProfile : Profile
{
public ToDoMappingProfile()
{
CreateMap<TodoItem, TodoItemDTO>();
}

}
}
46 changes: 46 additions & 0 deletions Migrations/20230713084140_InitialCreate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Migrations/20230713084140_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace TodoApiDTO.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TodoItems",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: true),
IsComplete = table.Column<bool>(nullable: false),
Secret = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_TodoItems", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TodoItems");
}
}
}
44 changes: 44 additions & 0 deletions Migrations/TodoContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using TodoApi.Models;

namespace TodoApiDTO.Migrations
{
[DbContext(typeof(TodoContext))]
partial class TodoContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.0")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("TodoApi.Models.TodoItem", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<bool>("IsComplete")
.HasColumnType("bit");

b.Property<string>("Name")
.HasColumnType("nvarchar(max)");

b.Property<string>("Secret")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("TodoItems");
});
#pragma warning restore 612, 618
}
}
}
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

namespace TodoApi
{
Expand All @@ -17,7 +18,7 @@ public static void Main(string[] args)
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
Host.CreateDefaultBuilder(args).UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Expand Down
9 changes: 9 additions & 0 deletions Repositories/Entities/IToDoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TodoApi.Models;

namespace TodoApiDTO.Repositories.Entities
{
public interface IToDoRepository:IRepositoryBase<TodoItem>
{

}
}
12 changes: 12 additions & 0 deletions Repositories/Entities/ToDoRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TodoApi.Models;
using TodoApiDTO.Repositories.Entities;

namespace TodoApiDTO.Repositories.ToDo
{
public class ToDoRepository : RepositoryBase<TodoItem>, IToDoRepository
{
public ToDoRepository(TodoContext repositoryContext) : base(repositoryContext)
{
}
}
}
15 changes: 15 additions & 0 deletions Repositories/IRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Linq.Expressions;
using System.Linq;
using System;

namespace TodoApiDTO.Repositories
{
public interface IRepositoryBase<T>
{
IQueryable<T> FindAll();
IQueryable<T> FindByCondition(Expression<Func<T, bool>> expression);
void Create(T entity);
void Update(T entity);
void Delete(T entity);
}
}
Loading