WeatherForecastController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Text.Json;
  5. using System.Text.Json.Serialization;
  6. using Microsoft.AspNetCore.Http.Metadata;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Data.SqlClient;
  9. namespace WebApplication1.Controllers
  10. {
  11. [ApiController]
  12. [Route("[controller]")]
  13. public class WeatherForecastController : ControllerBase
  14. {
  15. SqlConnection SqlConnection = new SqlConnection("Data Source=ngknn.ru;Initial Catalog=AAAnobldemo;User ID=21p;Password=12357;Connect Timeout=30;Encrypt=False;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False");
  16. [HttpGet(Name = "GetRegion")]
  17. [Tags("GetRegion")]
  18. public List<region> GetRegion()
  19. {
  20. SqlConnection.Open();
  21. SqlCommand sqlCommand = new SqlCommand("select NameRegion from Region", SqlConnection);
  22. List<region> list = new List<region>();
  23. SqlDataReader reader = sqlCommand.ExecuteReader();
  24. while (reader.Read())
  25. {
  26. region zalupa = new region();
  27. zalupa.Name = reader.GetString(0);
  28. list.Add(zalupa);
  29. }
  30. reader.Close();
  31. SqlConnection.Close();
  32. return list;
  33. }
  34. [HttpGet("GetReciplentMessage")]
  35. [Tags("GetWeatherForecast")]
  36. public List<ReciplentMessage> GetReciplentMessage()
  37. {
  38. SqlConnection.Open();
  39. SqlCommand sqlCommand = new SqlCommand("select RecipientMessage from RecipientMessage", SqlConnection);
  40. List<ReciplentMessage> list = new List<ReciplentMessage>();
  41. SqlDataReader reader = sqlCommand.ExecuteReader();
  42. while (reader.Read())
  43. {
  44. ReciplentMessage reciplentMessage = new ReciplentMessage();
  45. reciplentMessage.Name = reader.GetString(0);
  46. list.Add(reciplentMessage);
  47. }
  48. reader.Close();
  49. SqlConnection.Close();
  50. return list;
  51. }
  52. [HttpGet("GetUserRole")]
  53. [Tags("GetUserRole")]
  54. public IActionResult GetUserRole(string login, string password)
  55. {
  56. SqlConnection.Open();
  57. SqlCommand sqlCommand = new SqlCommand($"select id_role from role where login = '{login}' and password = '{password}'", SqlConnection);
  58. SqlDataReader reader = sqlCommand.ExecuteReader();
  59. if (reader.Read())
  60. {
  61. reader.Close();
  62. SqlConnection.Close();
  63. return Ok();
  64. }
  65. else
  66. {
  67. reader.Close();
  68. SqlConnection.Close();
  69. return Unauthorized();
  70. }
  71. }
  72. [HttpGet("UserUpdate")]
  73. [Tags("UserUpdate")]
  74. public bool UserUpdate(string? email, string? Area, string? city, string? street, string? housenymber, string? HouseBody, string? appartament, string? surname, string? name, string? patronomic, string? phone, string? socialstatus)
  75. {
  76. SqlConnection.Open();
  77. SqlCommand cmd = new SqlCommand($"insert into [User] values ('{email}','{Area}','{city}','{street}','{housenymber}','{HouseBody}','{appartament}','{surname}','{name}','{patronomic}','{phone}','{socialstatus}',NULL)", SqlConnection);
  78. if (cmd.ExecuteNonQuery() > 0)
  79. {
  80. SqlConnection.Close();
  81. return true;
  82. }
  83. else
  84. {
  85. SqlConnection.Close();
  86. return false;
  87. }
  88. }
  89. [HttpGet("FileUpdate")]
  90. [Tags("FileUpdate")]
  91. public void FileUpdate(byte[] formFile)
  92. {
  93. SqlConnection.Open();
  94. var cmd = new SqlCommand("INSERT INTO [File] ([File]) VALUES (@file)", SqlConnection);
  95. cmd.Parameters.Add("@file", SqlDbType.VarBinary).Value = formFile;
  96. if (cmd.ExecuteNonQuery() > 0)
  97. {
  98. SqlConnection.Close();
  99. }
  100. else
  101. {
  102. SqlConnection.Close();
  103. }
  104. }
  105. [HttpGet("AppealUpdate")]
  106. [Tags("AppealUpdate")]
  107. public bool AppealUpdate(string region, string? index, string RecipientMessage, string? text)
  108. {
  109. SqlConnection.Open();
  110. SqlCommand cmd = new SqlCommand($"insert into [Appeal] values ((select Region.idRegion from Region where NameRegion = '{region}'),'{index}',(select IdRecipientMessage from RecipientMessage where RecipientMessage = '{RecipientMessage}' ),'{text}',(SELECT MAX([File].IdFile) FROM [File]),(SELECT MAX(id_user) from [User]))", SqlConnection);
  111. cmd.ExecuteScalar();
  112. SqlConnection.Close();
  113. return true;
  114. }
  115. [HttpGet("RegionUpdate")]
  116. [Tags("RegionUpdate")]
  117. public void RegionUpdate(string? region)
  118. {
  119. SqlConnection.Open();
  120. SqlCommand cmd = new SqlCommand($" insert into Region values ('{region}') ", SqlConnection);
  121. cmd.ExecuteScalar();
  122. SqlConnection.Close();
  123. }
  124. [HttpGet("resipleUpdate")]
  125. [Tags("resipleUpdate")]
  126. public void resipleUpdate(string RecipientMessage)
  127. {
  128. SqlConnection.Open();
  129. SqlCommand cmd = new SqlCommand($" insert into RecipientMessage values ('{RecipientMessage}') ", SqlConnection);
  130. cmd.ExecuteScalar();
  131. SqlConnection.Close();
  132. }
  133. }
  134. public class region
  135. {
  136. public string Name { get; set; }
  137. }
  138. public class ReciplentMessage
  139. {
  140. public string Name { get; set; }
  141. }
  142. }