-
Notifications
You must be signed in to change notification settings - Fork 0
ExpenseController su [HttpPost] Post() metodu #4
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| bin/ | ||
| obj/ | ||
| .vs | ||
| .vs | ||
| ExpensesWebApp.csproj.user |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,29 +2,40 @@ | |
| using System.Linq; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Logging; | ||
| using ExpensesWebApp.Utilities; | ||
|
|
||
| namespace ExpensesWebApp.Controllers | ||
| { | ||
| [Route("api/[controller]")] | ||
| [ApiController] | ||
|
|
||
| public class ExpenseController : ControllerBase | ||
|
|
||
| { | ||
| private readonly ILogger<ExpenseController> _logger; | ||
|
|
||
| public ExpenseController (ILogger<ExpenseController> logger) | ||
| public IExpenseManager _expenseManager; | ||
|
|
||
| public ExpenseController(ILogger<ExpenseController> logger, IExpenseManager expenseManager) | ||
| { | ||
| _logger = logger; | ||
| _expenseManager = expenseManager; | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public IEnumerable<Expense> Get() | ||
| public IEnumerable<ExpenseItem> Get() | ||
| { | ||
| return Enumerable.Range(1, 1).Select(index => new Expense | ||
| return Enumerable.Range(1, 1).Select(index => new ExpenseItem | ||
| { | ||
| ExpenseSum = 10 | ||
| Expenses = 10 | ||
| }) | ||
| .ToArray(); | ||
| } | ||
| [HttpPost] | ||
| public void Post (ExpenseItem expenseItem) | ||
| { | ||
| _expenseManager.Insert(expenseItem); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dar pridėk patikrinimus, ar atsiųsti laukai validūs. Tarkim Expenses kad būtų > 0. Expense_type_ID ir User_ID gal vėliau padarysim kad pačioj duombazės patikrintų (Uždėsim duombazės foreign keys). |
||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace ExpensesWebApp | ||
| { | ||
| public class ExpenseItem | ||
| { | ||
| public DateTime ExpenseDate { get; set; } | ||
| public double Expenses { get; set; } | ||
| public int Expense_type_ID { get; set; } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vengiam visur underscore varduose. Pakeisk į ExpenseTypeId ir UserID. Aišku kai kas gal ir taip rašo, bet dažniausiai tai underscore nenaudoja. |
||
| public int User_ID { get; set; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| using System.Data.SqlClient; | ||
| using System.IO; | ||
| using System.Reflection; | ||
| using ExpensesWebApp.Utilities; | ||
|
|
||
| namespace ExpensesWebApp | ||
| { | ||
|
|
@@ -16,13 +17,14 @@ public Startup(IConfiguration configuration) | |
| { | ||
| Configuration = configuration; | ||
| } | ||
|
|
||
| public IConfiguration Configuration { get; } | ||
|
|
||
| // This method gets called by the runtime. Use this method to add services to the container. | ||
| public void ConfigureServices(IServiceCollection services) | ||
| { | ||
| services.AddControllers(); | ||
| services.AddSingleton(typeof(IConnectionToDB), typeof(ConnectionToDB)); | ||
| services.AddTransient(typeof(IExpenseManager), typeof(ExpenseManager)); | ||
|
|
||
| } | ||
|
|
||
|
|
@@ -53,9 +55,8 @@ public void InitializeDatabase() | |
| var assembly = Assembly.GetExecutingAssembly(); | ||
| var allResourceNames = assembly.GetManifestResourceNames(); | ||
| var resourceName = allResourceNames[0]; | ||
| string connStr = Configuration.GetConnectionString("DatabaseConnectionString"); | ||
|
|
||
| using var conn = new SqlConnection(connStr); | ||
| using var conn = new SqlConnection(Configuration.GetConnectionString("DatabaseConnectionString")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Čia panaudok tą patį ConnectionToDB iš dependency injection |
||
| using Stream stream = assembly.GetManifestResourceStream(resourceName); | ||
| using StreamReader reader = new StreamReader(stream); | ||
| conn.Open(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
| using System.Data.SqlClient; | ||
|
|
||
|
|
||
| namespace ExpensesWebApp.Utilities | ||
|
|
||
| { | ||
| public class ConnectionToDB : IConnectionToDB | ||
| { | ||
| public IConfiguration Configuration { get; } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Private šitie visur |
||
| public ConnectionToDB(IConfiguration configuration) | ||
| { | ||
| Configuration = configuration; | ||
| } | ||
| public SqlConnection Connect() | ||
| { | ||
| var conn = new SqlConnection(Configuration.GetConnectionString("DatabaseConnectionString")); | ||
| conn.Open(); | ||
| return conn; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System.Data.SqlClient; | ||
|
|
||
| namespace ExpensesWebApp.Utilities | ||
| { | ||
| public class ExpenseManager : IExpenseManager | ||
| { | ||
| public IConnectionToDB _connection; | ||
| public ExpenseManager(IConnectionToDB connection) | ||
| { | ||
| _connection = connection; | ||
| } | ||
| public void Insert(ExpenseItem expenseItem) | ||
| { | ||
| string query = "USE Expenses "; | ||
| query += "INSERT INTO dbo.expenses (date, expenses, expense_type_ID, user_ID) "; | ||
| query += "VALUES (@date, @expenses, @expense_type_ID, @user_ID)"; | ||
| var insert = new SqlCommand(query, _connection.Connect()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. čia _connection.Connect() sukuria objektą kurį reikia dispose'int. |
||
| insert.Parameters.AddWithValue("@date", expenseItem.ExpenseDate); | ||
| insert.Parameters.AddWithValue("@expenses", expenseItem.Expenses); | ||
| insert.Parameters.AddWithValue("@expense_type_ID", expenseItem.Expense_type_ID); | ||
| insert.Parameters.AddWithValue("@user_ID", expenseItem.User_ID); | ||
| insert.ExecuteNonQuery(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
| using System.Data.SqlClient; | ||
|
|
||
| namespace ExpensesWebApp.Utilities | ||
| { | ||
| public interface IConnectionToDB | ||
| { | ||
| IConfiguration Configuration { get; } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Čia nereikia configuration pridėt. Kam reikės tas pasiims configuration iš dependency injection. Palik tik Connect() interfeise. |
||
|
|
||
| SqlConnection Connect(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace ExpensesWebApp.Utilities | ||
| { | ||
| public interface IExpenseManager | ||
| { | ||
| void Insert(ExpenseItem expenseItem); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Šituos darom private, nėra prasmės public palikt.