Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/21 #30

Merged
merged 3 commits into from
Mar 17, 2022
Merged
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
10 changes: 10 additions & 0 deletions Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Mvc;

namespace p_designer.controllers
{
[ApiController]
[Route("api")]
public class BaseController : ControllerBase
{
}
}
33 changes: 33 additions & 0 deletions Controllers/DictionaryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
using p_designer.entities;
using p_designer.Models;
using p_designer.services;
using Swashbuckle.AspNetCore.Annotations;

namespace p_designer.controllers
{
public class DictionaryController : BaseController
{
private DictionaryService dictionaryService { get; set; }
public DictionaryController(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
}

[HttpGet]
[Route("lifecycle-statuses")]
[SwaggerOperation(Summary = "Статусы жизненного цикла")]
public async Task<IQueryable<DictionaryModel>> GetLifecycleStatuses()
{
return await dictionaryService.GetLifecycleStatusesAsync();
}

[HttpGet]
[Route("aspect-levels")]
[SwaggerOperation(Summary = "Уровни аспектов")]
public async Task<IQueryable<DictionaryModel>> GetAspectLevels()
{
return await dictionaryService.GetAspectLevelsAsync();
}
}
}
66 changes: 66 additions & 0 deletions Controllers/PatternController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Microsoft.AspNetCore.Mvc;
using p_designer.Models;
using p_designer.services;
using Swashbuckle.AspNetCore.Annotations;
using System.ComponentModel.DataAnnotations;

namespace p_designer.controllers
{
public class PatternController : BaseController
{
private PatternService patternService { get; set; }

public PatternController(PatternService patternService)
{
this.patternService = patternService;
}

[HttpGet]
[Route("patterns")]
[SwaggerOperation(Summary = "Получить страницу паттернов")]
public async Task<MetaDataModel<PatternModel.Read.Short>> ReadPatternsPage([Required] int page, [Required] int pageSize)
{
return await patternService.ReadPageAsync(page, pageSize);
}

[HttpGet]
[Route("pattern")]
[SwaggerOperation(Summary = "Получить паттерн")]
public async Task<PatternModel.Read.Long> ReadPattern([Required] int id)
{
return await patternService.ReadAsync(id);
}

[HttpPost]
[Route("pattern")]
[SwaggerOperation(Summary = "Создать паттерн")]
public async Task CreatePattern(PatternModel.Create pattern)
{
await patternService.CreateAsync(pattern);
}

[HttpPut]
[Route("pattern")]
[SwaggerOperation(Summary = "Изменить паттерн")]
public async Task UpdatePattern(PatternModel.Update pattern)
{
await patternService.UpdateAsync(pattern);
}

[HttpDelete]
[Route("pattern/remove")]
[SwaggerOperation(Summary = "Изменить статус паттерна на deleted")]
public async Task RemoveAsync([Required] int id)
{
await patternService.RemoveAsync(id);
}

[HttpDelete]
[Route("pattern/delete")]
[SwaggerOperation(Summary = "Удалить паттерн из базы данных")]
public async Task DeleteAsync([Required] int id)
{
await patternService.DeleteAsync(id);
}
}
}
8 changes: 8 additions & 0 deletions Models/DictionaryModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace p_designer.Models
{
public class DictionaryModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}
17 changes: 17 additions & 0 deletions Models/MetaDataModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace p_designer.Models
{
public class MetaDataModel<T>
{
public IEnumerable<T> Data { get; set; }
public Meta Meta { get; set; }
}

public class Meta
{
public int TotalItemsNumber { get; set; }
public int PagesNumber { get; set; }
public int CurrentPage { get; set; }
public bool HasNextPage { get; set; }
public bool HasPreviousPage { get; set; }
}
}
43 changes: 43 additions & 0 deletions Models/PatternModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace p_designer.Models
{
public class PatternModel
{
public class Read
{
public class Long
{
public int Id { get; set; }
public string Name { get; set; }
public int LifecycleStatusId { get; set; }
public double ProjectValueMin { get; set; }
public double ProjectValueMax { get; set; }
public double ProjectValueTarget { get; set; }
}

public class Short
{
public int Id { get; set; }
public string Name { get; set; }
}
}

public class Create
{
public string Name { get; set; }
public int LifecycleStatusId { get; set; }
public double ProjectValueMin { get; set; }
public double ProjectValueMax { get; set; }
public double ProjectValueTarget { get; set; }
}

public class Update
{
public int Id { get; set; }
public string Name { get; set; }
public int LifecycleStatusId { get; set; }
public double ProjectValueMin { get; set; }
public double ProjectValueMax { get; set; }
public double ProjectValueTarget { get; set; }
}
}
}
12 changes: 12 additions & 0 deletions Models/enums/AspectLevelEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace p_designer.Models.enums
{
public enum AspectLevelEnum
{
BusinessLayer = 1,
FunctionalLayer = 2,
InformationLayer = 3,
CommunicationLayer = 4,
IntegrationLayer = 5,
PhysicalLayer = 6
}
}
9 changes: 9 additions & 0 deletions Models/enums/LifecycleStatusEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace p_designer.Models.enums
{
public enum LifecycleStatusEnum
{
Draft = 1,
ReadyToUse = 2,
Deleted = 3
}
}
30 changes: 29 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.OpenApi.Models;
using p_designer.entities;
using p_designer.services;
using Swashbuckle.AspNetCore.SwaggerUI;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -11,13 +14,38 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<PDesignerContext>();

builder.Services.AddTransient<DictionaryService>();
builder.Services.AddTransient<PatternService>();

builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "p-designer", Version = "v1" });
c.CustomSchemaIds(type => type.ToString());
c.EnableAnnotations();
});

var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(x =>
{
x.SwaggerEndpoint("/swagger/v1/swagger.json", "p-designer API v1");
x.DefaultModelExpandDepth(3);
x.DefaultModelRendering(ModelRendering.Example);
x.DefaultModelsExpandDepth(-1);
x.DisplayOperationId();
x.DisplayRequestDuration();
x.DocExpansion(docExpansion: DocExpansion.None);
x.EnableDeepLinking();
x.EnableFilter();
x.ShowExtensions();
});
}

app.UseHttpsRedirection();
Expand Down
26 changes: 26 additions & 0 deletions Services/DictionaryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Mapster;
using p_designer.entities;
using p_designer.Models;

namespace p_designer.services
{
public class DictionaryService
{
private PDesignerContext context { get; set; }

public DictionaryService(PDesignerContext context)
{
this.context = context;
}

public Task<IQueryable<DictionaryModel>> GetLifecycleStatusesAsync()
{
return Task.FromResult(context.LifecycleStatuses.ProjectToType<DictionaryModel>());
}

public Task<IQueryable<DictionaryModel>> GetAspectLevelsAsync()
{
return Task.FromResult(context.AspectLevels.ProjectToType<DictionaryModel>());
}
}
}
62 changes: 62 additions & 0 deletions Services/PatternService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Mapster;
using Microsoft.EntityFrameworkCore;
using p_designer.common;
using p_designer.common.extensions;
using p_designer.entities;
using p_designer.Models;
using p_designer.Models.enums;

namespace p_designer.services
{
public class PatternService
{
private PDesignerContext context { get; set; }
public PatternService(PDesignerContext context)
{
this.context = context;
}

public async Task<PatternModel.Read.Long> ReadAsync(int id)
{
return await context.Patterns
.ProjectToType<PatternModel.Read.Long>()
.SingleAsync(p => p.Id == id);
}

public async Task<MetaDataModel<PatternModel.Read.Short>> ReadPageAsync(int page, int pageSize)
{
var data = context.Patterns.Where(p => p.LifecycleStatusId != (int)LifecycleStatusEnum.Deleted)
.ProjectToType<PatternModel.Read.Short>();
var factory = new MetaDataFactory<PatternModel.Read.Short>(data);
return await factory.CreateAsync(page, pageSize);
}

public async Task CreateAsync(PatternModel.Create patternModel)
{
var pattern = patternModel.Adapt<Pattern>();
await context.AddAsync(pattern);
await context.SaveChangesAsync();
}

public async Task UpdateAsync(PatternModel.Update patternModel)
{
var pattern = patternModel.Adapt<PatternModel.Update>();
context.Update(pattern);
await context.SaveChangesAsync();
}

public async Task RemoveAsync(int id)
{
var pattern = await context.Patterns.FindAsync(id);
pattern.LifecycleStatusId = (int)LifecycleStatusEnum.Deleted;
await context.SaveChangesAsync();
}

public async Task DeleteAsync(int id)
{
var pattern = await context.Patterns.FindAsync(id);
context.Remove(pattern);
await context.SaveChangesAsync();
}
}
}
34 changes: 34 additions & 0 deletions common/MetaDataFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using p_designer.common;
using p_designer.common.extensions;
using p_designer.Models;

namespace p_designer.common
{
public class MetaDataFactory<T>
{
private IQueryable<T> data { get; set; }
public MetaDataFactory(IQueryable<T> data)
{
this.data = data;
}

public async Task<MetaDataModel<T>> CreateAsync(int page, int pageSize)
{
var meta = new Meta();
var pagedData = data.GetPage(page, pageSize);

meta.TotalItemsNumber = await data.CountAsync();
meta.PagesNumber = PagesCounter.GetPagesNumber(pageSize, meta.TotalItemsNumber);
meta.CurrentPage = page;
meta.HasNextPage = meta.CurrentPage < meta.PagesNumber;
meta.HasPreviousPage = meta.CurrentPage > 1;

return new MetaDataModel<T>()
{
Data = pagedData,
Meta = meta
};
}
}
}
13 changes: 13 additions & 0 deletions common/PagesCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace p_designer.common
{
public static class PagesCounter
{
public static int GetPagesNumber(int pagesSize, int totalItemsNumber)
{
var pages = totalItemsNumber / pagesSize + 1;
if (totalItemsNumber % pagesSize == 0)
pages--;
return pages;
}
}
}
Loading