Program.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using MySqlConnector;
  2. using Dapper;
  3. var builder = WebApplication.CreateBuilder(args);
  4. var app = builder.Build();
  5. var dbDataProvider = new DBDataProvider();
  6. app.MapGet("/product", (int? pageNum) =>
  7. {
  8. return dbDataProvider.GetProducts(pageNum ?? 1);
  9. });
  10. app.MapPost("/product", (Product newProduct) =>
  11. {
  12. dbDataProvider.saveProduct(newProduct);
  13. });
  14. app.MapDelete("/product/{id:int}", (int id) =>
  15. {
  16. dbDataProvider.removeProduct(id);
  17. });
  18. app.MapPut("/product", (Product editProduct) =>
  19. {
  20. dbDataProvider.saveProduct(editProduct);
  21. });
  22. app.MapGet("/productCount", () =>
  23. {
  24. return dbDataProvider.GetProductCount();
  25. });
  26. app.MapPut("/minCostForAgent/{minCost:decimal}", (decimal minCost, int[] ids) =>
  27. {
  28. dbDataProvider.setMinCostForAgent(minCost, ids);
  29. });
  30. app.MapGet("/articleCheck/{article}/{ID:int}", (string article, int ID) =>
  31. {
  32. return dbDataProvider.getArticleCheck(article, ID);
  33. });
  34. app.MapGet("/saleCount/{ID:int}", (int ID) =>
  35. {
  36. return dbDataProvider.saleCount(ID);
  37. });
  38. app.MapDelete("/productMaterial/{ID:int}", (int ID) =>
  39. {
  40. dbDataProvider.removeProductMaterial(ID);
  41. });
  42. app.MapDelete("/productCostHistory/{ID:int}", (int ID) =>
  43. {
  44. dbDataProvider.removeProductCostHistory(ID);
  45. });
  46. app.MapGet("/productMaterial/{ID:int}", (int ID) =>
  47. {
  48. return dbDataProvider.getProductMaterials(ID);
  49. });
  50. app.MapGet("/material", () =>
  51. {
  52. return dbDataProvider.getMaterials();
  53. });
  54. app.MapPost("/productMaterial", (ProductMaterial material) =>
  55. {
  56. dbDataProvider.addProductMaterial(material);
  57. });
  58. app.Run();
  59. public class DBDataProvider
  60. {
  61. public string connectionString = "Server=kolei.ru; User ID=ygrebnev; Password=150406; Database=ygrebnev";
  62. public List<Product> GetProducts(int pageNum)
  63. {
  64. using (MySqlConnection db = new MySqlConnection(connectionString))
  65. {
  66. var builder = new SqlBuilder();
  67. // äîáàâëåíèå ñîðòèðîâêè â çàïðîñ
  68. if (orderCondition.Length > 0)
  69. builder.OrderBy(orderCondition);
  70. // äîáàâëåíèå ôèëüòðàöèè â çàïðîñ
  71. if (filters.Count > 0)
  72. {
  73. foreach (var item in filters)
  74. builder.Where(item.Key, item.Value);
  75. }
  76. // ñîñòàâëåíèå øàáëîíà çàïðîñà
  77. var template = builder.AddTemplate(
  78. "SELECT * FROM ygrebnev.porevo228 /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
  79. new
  80. {
  81. pageLen = 20,
  82. offset = (pageNum - 1) * 20
  83. }
  84. );
  85. // âûïîëíåíèå çàïðîñà
  86. return db.Query<Product>
  87. (
  88. template.RawSql,
  89. template.Parameters).ToList();
  90. }
  91. }
  92. private string orderCondition = "";
  93. public void setOrder(string condition)
  94. {
  95. orderCondition = condition;
  96. }
  97. private Dictionary<string, object> filters = new Dictionary<string, object>();
  98. public void addFilter(string name, object value)
  99. {
  100. filters.Add(name, value);
  101. }
  102. public void clearFilter()
  103. {
  104. filters.Clear();
  105. }
  106. public void saveProduct(Product product)
  107. {
  108. using (MySqlConnection db = new MySqlConnection(connectionString))
  109. {
  110. if (product.ID == 0)
  111. {
  112. db.Execute("INSERT INTO Product" +
  113. "(`Title`, `ProductTypeID`,`ArticleNumber`,`Description`,`Image`," +
  114. "`ProductionPersonCount`,`ProductionWorkshopNumber`,`MinCostForAgent`) " +
  115. "VALUES (@Title,@ProductTypeID,@ArticleNumber,@Description,@Image," +
  116. "@ProductionPersonCount,@ProductionWorkshopNumber,@MinCostForAgent);", product);
  117. }
  118. else
  119. {
  120. db.Execute("UPDATE Product SET Title=@Title, ProductTypeID=@ProductTypeID, " +
  121. "ArticleNumber=@ArticleNumber, Description=@Description, " +
  122. "Image=@Image, ProductionPersonCount=@ProductionPersonCount, " +
  123. "ProductionWorkshopNumber=@ProductionWorkshopNumber, " +
  124. "MinCostForAgent=@MinCostForAgent " +
  125. "WHERE ID=@ID", product);
  126. }
  127. }
  128. }
  129. public void removeProduct(int ID)
  130. {
  131. using (MySqlConnection db = new MySqlConnection(connectionString))
  132. {
  133. db.Execute("DELETE FROM Product WHERE ID = @ID", new { ID = ID });
  134. }
  135. }
  136. public int GetProductCount()
  137. {
  138. using (MySqlConnection db = new MySqlConnection(connectionString))
  139. {
  140. var builder = new SqlBuilder();
  141. if (filters.Count > 0)
  142. {
  143. foreach (var item in filters)
  144. builder.Where(item.Key, item.Value);
  145. }
  146. var template = builder.AddTemplate(
  147. "SELECT count(*) FROM porevo228 /**where**/");
  148. return db.QuerySingle<int>(
  149. template.RawSql,
  150. template.Parameters);
  151. }
  152. }
  153. public void setMinCostForAgent(decimal minCostForAgent, int[] ids)
  154. {
  155. using (MySqlConnection db = new MySqlConnection(connectionString))
  156. {
  157. db.Execute("UPDATE Product SET MinCostForAgent=@newCost WHERE ID in @idList", new
  158. {
  159. newCost = minCostForAgent,
  160. idList = ids
  161. });
  162. }
  163. }
  164. public int getArticleCheck(string article, int ID)
  165. {
  166. using (MySqlConnection db = new MySqlConnection(connectionString))
  167. {
  168. return db.QuerySingle<int>("SELECT count(*) FROM Product " +
  169. "WHERE ArticleNumber=@article AND ID != @ID;", new
  170. {
  171. article = article,
  172. ID = ID
  173. });
  174. }
  175. }
  176. public int saleCount(int ID)
  177. {
  178. using (MySqlConnection db = new MySqlConnection(connectionString))
  179. {
  180. if (ID != 0)
  181. {
  182. return db.QuerySingle<int>("SELECT count(*) FROM ProductSale WHERE ProductID=@ID", new { ID = ID });
  183. }
  184. return 0;
  185. }
  186. }
  187. public void removeProductMaterial(int ID)
  188. {
  189. using (MySqlConnection db = new MySqlConnection(connectionString))
  190. {
  191. db.Execute("DELETE FROM ProductMaterial WHERE ProductID=@ID", new { ID = ID });
  192. }
  193. }
  194. public void removeProductCostHistory(int ID)
  195. {
  196. using (MySqlConnection db = new MySqlConnection(connectionString))
  197. {
  198. db.Execute("DELETE FROM ProductCostHistory WHERE ProductID = @ID", new { ID = ID });
  199. }
  200. }
  201. public List<ProductMaterial> getProductMaterials(int ID)
  202. {
  203. using (MySqlConnection db = new MySqlConnection(connectionString))
  204. {
  205. return db.Query<ProductMaterial>("SELECT pm.*,m.Title " +
  206. "FROM ProductMaterial as pm,Material as m " +
  207. "WHERE pm.ProductID = @ID AND m.ID = pm.MaterialID", new { ID = ID }).ToList();
  208. }
  209. }
  210. public void deleteProductMaterial(ProductMaterial material)
  211. {
  212. using (MySqlConnection db = new MySqlConnection(connectionString))
  213. {
  214. db.Execute("DELETE FROM ProductMaterial WHERE ProductID = @ProductId AND MaterialID = @MaterialId", material);
  215. }
  216. }
  217. public List<Material> getMaterials()
  218. {
  219. using (MySqlConnection db = new MySqlConnection(connectionString))
  220. {
  221. return db.Query<Material>("SELECT ID,Title FROM ygrebnev.Material").ToList();
  222. }
  223. }
  224. public void addProductMaterial(ProductMaterial material)
  225. {
  226. using (MySqlConnection db = new MySqlConnection(connectionString))
  227. {
  228. db.Execute("INSERT INTO ProductMaterial (`ProductID`,`MaterialID`,`Count`) VALUES (@ProductId,@MaterialId,@Count)", material);
  229. }
  230. }
  231. }
  232. public class Product
  233. {
  234. public int ID { get; set; }
  235. public string Title { get; set; }
  236. public int ProductTypeID { get; set; }
  237. public string ProductTypeTitle { get; set; }
  238. public string ArticleNumber { get; set; }
  239. public string Description { get; set; }
  240. public string Image { get; set; }
  241. public int ProductionPersonCount { get; set; }
  242. public int ProductionWorkshopNumber { get; set; }
  243. public decimal MinCostForAgent { get; set; }
  244. public int MaterialCost { get; set; }
  245. public string MaterialString { get; set; }
  246. public int? LastMonthSaleQuantity { get; set; }
  247. }
  248. public class ProductMaterial
  249. {
  250. public int ProductId { get; set; }
  251. public int MaterialId { get; set; }
  252. public double Count { get; set; }
  253. }
  254. public class Material
  255. {
  256. public int ID { get; set; }
  257. public string Title { get; set; }
  258. }