


Космос и Вселенная
+4По дате
По рейтингу
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/orders", () =>
{
using Repository repo = new Repository();
return repo.ReadAll();
});
app.MapGet("/orders/{id}", (Guid id) =>
{
using Repository repo = new Repository();
return repo.Read(id);
});
app.MapPost("/orders",(CreateOrderDTO dto) =>
{
using Repository repo = new Repository();
Order order = new Order(dto.Device, dto.ProblemType, dto.Description, dto.Client);
repo.Add(order);
repo.SaveChanges();
});
app.MapPut("/orders/{id}", (UpdateOrderDTO dto,Guid id) =>
{
using Repository repo = new Repository();
repo.Update(dto, id);
repo.SaveChanges();
});
app.MapGet("/statistics", () =>
{
using Repository repo = new Repository();
var competeCount = repo.GetCompleteCount();
var averageTime = repo.GetAverageTime();
var stat = repo.GetStatistics();
StatisticDTO statistic = new StatisticDTO(competeCount,averageTime,stat);
return statistic;
});
app.Run();
class Order
{
public Order(string device, string problemType, string description, string client)
{
Id = Guid.NewGuid();
StartDate = DateTime.Now;
EndDate = null;
Device = device;
ProblemType = problemType;
Description = description;
Client = client;
Status = "в ожидании";
Worker = "не назначен";
Comment = "";
}
public Guid Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string Device { get; set; }
public string ProblemType { get; set; }
public string Description { get; set; }
public string Client { get; set; }
public string status;
public string Status {
get => status;
set
{
if(value=="выполнено")
EndDate = DateTime.Now;
status = value;
}
}
public string Worker { get; set; }
public string Comment { get; set; }
}
record class CreateOrderDTO(
string Device,
string ProblemType,
string Description,
string Client);
record class UpdateOrderDTO(
string Status,
string Description,
string Worker,
string Comment);
class StatisticDTO(int completeCount, double averageTime, Dictionary<string, int> stat)
{
public int CompleteCount { get; set; } = completeCount;
public double AverageTime { get; set; } = averageTime;
public Dictionary<string, int> Stat { get; set; } = stat;
}
class Repository : DbContext
{
private DbSet<Order> Orders { get; set; }
public Repository()
{
Orders = Set<Order>();
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=orders.db");
}
public void Add(Order order)
{
Orders.Add(order);
}
public Order Read(Guid id)
{
return Orders.Find(id);
}
public List<Order> ReadAll()
{
return Orders.ToList();
}
public void Update(UpdateOrderDTO dto, Guid id)
{
Order order = Read(id);
if(dto.Status != order.Status)
order.Status = dto.Status;
if(dto.Description != order.Description)
order.Description = dto.Description;
if(dto.Worker != order.Worker)
order.Worker = dto.Worker;
if(dto.Comment != order.Comment)
order.Comment = dto.Comment;
}
public int GetCompleteCount()
{
return Orders.Count(x => x.Status == "выполнено");
}
public double GetAverageTime()
{
List<Order> completeOrders = Orders.ToList().FindAll(x => x.Status == "выполнено");
if (completeOrders.Count == 0)
return 0;
double allTime = 0;
foreach (Order order in completeOrders)
allTime += (order.EndDate - order.StartDate).Value.Hours;
return allTime/completeOrders.Count;
}
public Dictionary<string, int> GetStatistics()
{
Dictionary<string,int> result = new Dictionary<string, int>();
foreach (Order order in Orders.ToList())
{
if(result.ContainsKey(order.ProblemType))
result[order.ProblemType]++;
else
result[order.ProblemType] = 1;
}
return result;
}
}