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

Added commands for order #25

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
5 changes: 2 additions & 3 deletions Alligator.BusinessLayer/Configuration/CustomMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Alligator.BusinessLayer.Models;
using Alligator.DataLayer;
using Alligator.DataLayer.Entities;
using AutoMapper;

Expand All @@ -22,9 +23,7 @@ private static void InitializeInstance()
cfg.CreateMap<ProductTag, ProductTagModel>();
cfg.CreateMap<ProductTagModel, ProductTag>();
cfg.CreateMap<Category, CategoryModel>();
cfg.CreateMap<CategoryModel, Category>();
cfg.CreateMap<Order, OrderShortModel>();
cfg.CreateMap<OrderShortModel, Order>();
cfg.CreateMap<CategoryModel, Category>();
cfg.CreateMap<Order, OrderModel>();
cfg.CreateMap<OrderModel, Order>();
cfg.CreateMap<OrderDetail, OrderDetailModel>();
Expand Down
8 changes: 6 additions & 2 deletions Alligator.BusinessLayer/Models/OrderModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

namespace Alligator.BusinessLayer.Models
{
public class OrderModel: OrderShortModel
{
public class OrderModel
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string Address { get; set; }
public ClientModel Client { get; set; }
public List<OrderDetailModel> OrderDetails { get; set; }
public List<OrderReviewModel> OrderReviews { get; set; }
}
Expand Down
16 changes: 0 additions & 16 deletions Alligator.BusinessLayer/Models/OrderShortModel.cs

This file was deleted.

76 changes: 58 additions & 18 deletions Alligator.BusinessLayer/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,86 @@ public OrderService()
_repositoryOrderReview = new OrderReviewRepository();
}

public List<OrderShortModel> GetOrdersWithoutSensitiveData()
public ActionResult<List<OrderModel>> GetOrders()
{
var orders = _repositoryOrder.GetAllOrders();
return CustomMapper.GetInstance().Map<List<OrderShortModel>>(orders);
try
{
return new ActionResult<List<OrderModel>>(true, CustomMapper.GetInstance().Map<List<OrderModel>>(orders));
}
catch (Exception exception)
{
return new ActionResult<List<OrderModel>>(false, null) { ErrorMessage = exception.Message };
}
}

public List<OrderModel> GetOrders()
{
var orders = _repositoryOrder.GetAllOrders();
return CustomMapper.GetInstance().Map<List<OrderModel>>(orders);
}

public List<OrderModel> GetOrdersByClientId(int id)
public ActionResult<List<OrderModel>> GetOrdersByClientId(int id)
{
var orders = _repositoryOrder.GetOrdersByClientId(id);
return CustomMapper.GetInstance().Map<List<OrderModel>>(orders);
try
{
return new ActionResult<List<OrderModel>>(true, CustomMapper.GetInstance().Map<List<OrderModel>>(orders));
}
catch (Exception exception)
{
return new ActionResult<List<OrderModel>>(false, null) { ErrorMessage = exception.Message };
}
}

public OrderModel GetOrderByIdWithDetailsAndReviews(int id)
public ActionResult<OrderModel> GetOrderByIdWithDetailsAndReviews(int id)
{
var order = _repositoryOrder.GetOrderById(id);
order.OrderDetails = _repositoryOrderDetail.GetOrderDetailsByOrderId(id);
order.OrderReviews = _repositoryOrderReview.GetOrderReviewsByOrderId(id);
return CustomMapper.GetInstance().Map<OrderModel>(order);
try
{
return new ActionResult<OrderModel>(true, CustomMapper.GetInstance().Map<OrderModel>(order));
}
catch
(Exception exception)
{
return new ActionResult<OrderModel>(false, null) { ErrorMessage = exception.Message };

}
}

public int AddOrderModel(DateTime date, int clientId, string address)
{
int id =_repositoryOrder.AddOrder(date, clientId, address);
return id;
try
{
int id =_repositoryOrder.AddOrder(date, clientId, address);
return id;
}
catch
{
return -1;
}
}

public void DeleteOrderModel(int id)
public bool DeleteOrderModel(int id)
{
_repositoryOrder.DeleteOrder(id);
try
{
_repositoryOrder.DeleteOrder(id);
return true;
}
catch
{
return false;
}
}

public void EditOrderModel(DateTime date, int id, string address)
public bool EditOrderModel(DateTime date, int orderId, int clientId, string address)
{
_repositoryOrder.EditOrder(date, id, address);
try
{
_repositoryOrder.EditOrder(date, orderId, clientId, address);
return true;
}
catch
{
return false;
}
}
}
}
12 changes: 7 additions & 5 deletions Alligator.DB/Procedures/Order_SelectAll.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
AS
BEGIN
select
id,
date,
clientId,
Address
from dbo.[Order]
o.id,
o.date,
o.Address,
c.Id,
c.FirstName,
c.LastName
from dbo.[Order] o inner join dbo.[Client] c on o.ClientId=c.Id
END
4 changes: 3 additions & 1 deletion Alligator.DB/Procedures/Order_Update.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
create proc dbo.Order_Update
@Id int,
@ClientId int,
@Address varchar(200),
@Date date
AS
BEGIN
update dbo.[Order] set Address=@Address,
update dbo.[Order] set ClientId=@ClientId,
Address=@Address,
Date=@Date
where Id=@Id
END
7 changes: 4 additions & 3 deletions Alligator.DataLayer/Repositories/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.Data.SqlClient;

using System.Data.SqlClient;

namespace Alligator.DataLayer.Repositories
{
public abstract class BaseRepository
{
private const string _connection = "Data Source=80.78.240.16;Database=AggregatorAlligator;User Id=student;Password=qwe!23;";

protected static SqlConnection ProvideConnection() => new SqlConnection(_connection);
public static SqlConnection ProvideConnection() => new SqlConnection(_connection);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поменяй обратно на протектед


protected enum AffectedRows
{
Zero,
One
}
}
}
}
2 changes: 1 addition & 1 deletion Alligator.DataLayer/Repositories/IOrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IOrderRepository
{
int AddOrder(DateTime date, int clientId, string address);
void DeleteOrder(int id);
void EditOrder(DateTime date, int id, string address);
void EditOrder(DateTime date, int orderId, int clientId, string address);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а объект ордера передать не судьба?)

List<Order> GetAllOrders();
Order GetOrderById(int id);
List<Order> GetOrdersByClientId(int id);
Expand Down
16 changes: 8 additions & 8 deletions Alligator.DataLayer/Repositories/OrderDetailRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public OrderDetail GetOrderDetailById(int id)
orderdetail.Product = product;
return orderdetail;
},
new { Id = id },
new { id },
commandType: CommandType.StoredProcedure,
splitOn: "Id")
.FirstOrDefault();
Expand All @@ -41,7 +41,7 @@ public List<OrderDetail> GetOrderDetailsByOrderId(int id)
orderdetail.Product = product;
return orderdetail;
},
new { OrderId = id },
new { orderId=id },
commandType: CommandType.StoredProcedure,
splitOn: "Id")
.Distinct().ToList();
Expand All @@ -58,7 +58,7 @@ public List<OrderDetail> GetOrderDetailsByProductId(int id)
orderdetail.Product = product;
return orderdetail;
},
new { ProductId = id },
new { id },
commandType: CommandType.StoredProcedure,
splitOn: "Id")
.Distinct().ToList();
Expand All @@ -69,38 +69,38 @@ public void AddOrderDetail(int amount, int orderId, int productId)
using var connection = ProvideConnection();
string procString = "dbo.OrderDetail_Insert";
connection.Execute(procString,
new { Amount = amount, OrdertId = orderId, ProductId = productId },
new { amount, orderId, productId },
commandType: CommandType.StoredProcedure);
}

public void EditOrderDetail(int id, int amount)
{
using var connection = ProvideConnection();
string procString = "dbo.OrderDetail_Update";
connection.Execute(procString, new { Id = id, Amount = amount },
connection.Execute(procString, new { id, amount },
commandType: CommandType.StoredProcedure);
}

public void DeleteOrderDetail(int id)
{
using var connection = ProvideConnection();
string procString = "dbo.OrderDetail_Delete";
connection.Execute(procString, new { Id = id },
connection.Execute(procString, new { id },
commandType: CommandType.StoredProcedure);
}

public void DeleteOrderDetailByOrderId(int id)
{
using var connection = ProvideConnection();
string procString = "dbo.OrderDetail_DeleteByOrderId";
connection.Execute(procString, new { OrderId=id },
connection.Execute(procString, new { orderId=id },
commandType: CommandType.StoredProcedure);
}
public void DeleteOrderDetailByProductId(int id)
{
using var connection = ProvideConnection();
string procString = "dbo.OrderDetail_DeleteByProductId";
connection.Execute(procString, new { ProductId = id },
connection.Execute(procString, new { id },
commandType: CommandType.StoredProcedure);
}
}
Expand Down
17 changes: 11 additions & 6 deletions Alligator.DataLayer/Repositories/OrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public List<Order> GetAllOrders()
{
using var connection = ProvideConnection();
var orderDictionary = new Dictionary<int, Order>();
return connection.Query<Order>("dbo.Order_SelectAll",
return connection.Query<Order, Client, Order>("dbo.Order_SelectAll", (order, client) =>
{
order.Client = client;

return order;
},
commandType: CommandType.StoredProcedure)
.ToList();
}
Expand All @@ -31,7 +36,7 @@ public Order GetOrderById(int id)

return order;
},
new { Id = id },
new { id },
commandType: CommandType.StoredProcedure,
splitOn: "Id").
FirstOrDefault();
Expand All @@ -47,7 +52,7 @@ public List<Order> GetOrdersByClientId(int id)
order.Client = client;
return order;
},
new { ClientId = id },
new { id },
commandType: CommandType.StoredProcedure,
splitOn: "Id").
Distinct().ToList();
Expand All @@ -58,7 +63,7 @@ public int AddOrder(DateTime date, int clientId, string address)
using var connection = ProvideConnection();
string procString = "dbo.Order_Insert";
return connection.QueryFirstOrDefault<int>(procString,
new { Date = date, ClientId = clientId, Address = address },
new { date,clientId, address },
commandType: CommandType.StoredProcedure);
}

Expand All @@ -71,12 +76,12 @@ public void DeleteOrder(int id)
commandType: CommandType.StoredProcedure);
}

public void EditOrder(DateTime date, int id, string address)
public void EditOrder(DateTime date, int orderId, int clientId, string address)
{
using var connection = ProvideConnection();
string procString = "dbo.Order_Update";
connection.Execute(procString,
new { Date = date, Id = id, Address = address },
new { date, Id=orderId, clientId, address },
commandType: CommandType.StoredProcedure);
}

Expand Down
14 changes: 6 additions & 8 deletions Alligator.DataLayer/Repositories/OrderReviewRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public OrderReview GetOrderReviewById(int id)
return connection.Query<OrderReview, Order, Client, OrderReview>
("dbo.OrderReview_SelectById", (orderreview, order, client) =>
{
orderreview.Order = order;
//orderreview.Client = client;
orderreview.Order = order;
return orderreview;
},
new { Id = id },
new { id },
commandType: CommandType.StoredProcedure,
splitOn: "Id").
FirstOrDefault();
Expand All @@ -36,10 +35,9 @@ public List<OrderReview> GetOrderReviewsByOrderId(int id)
("dbo.OrderReview_SelectByOrderId", (orderreview, order, client) =>
{
orderreview.Order = order;
//orderreview.Client = client;
return orderreview;
},
new { OrderId = id },
new { OrderId=id },
commandType: CommandType.StoredProcedure,
splitOn: "Id").
Distinct().ToList();
Expand All @@ -59,23 +57,23 @@ public void DeleteOrderReview(int id)
using var connection = ProvideConnection();
string procString = "dbo.OrderReview_Delete";
connection.Execute(procString,
new { Id = id },
new { id },
commandType: CommandType.StoredProcedure);
}

public void DeleteOrderReviewByOrderId(int orderId)
{
using var connection = ProvideConnection();
string procString = "dbo.OrderReview_DeleteByOrderId";
connection.Execute(procString, new { OrderId = orderId },
connection.Execute(procString, new { orderId },
commandType: CommandType.StoredProcedure);
}

public void EditOrderReview(int id, string text)
{
using var connection = ProvideConnection();
string procString = "dbo.OrderReview_Update";
connection.Execute(procString, new { Id = id, Text = text },
connection.Execute(procString, new { id, text },
commandType: CommandType.StoredProcedure);
}

Expand Down
Loading