Skip to content

ExpenseController su [HttpPost] Post() metodu - #4

Open
ljagintavicius wants to merge 2 commits into
masterfrom
HttpPost-update
Open

ExpenseController su [HttpPost] Post() metodu#4
ljagintavicius wants to merge 2 commits into
masterfrom
HttpPost-update

Conversation

@ljagintavicius

Copy link
Copy Markdown
Collaborator

Sukūriau metodą, kuris įkelia duomenis į db. ExpenseItem objektą sukūriau pačiam metodu, bet čia tik pradžiai, pasibandymui ar veikia.

Sukūriau metodą, kuris įkelia duomenis į db. ExpenseItem objektą sukūriau pačiam metodu, bet čia tik pradžiai, pasibandymui ar veikia.
Comment thread Controllers/ExpenseController.cs Outdated
Comment thread Controllers/ExpenseController.cs Outdated
Comment thread Controllers/ExpenseController.cs Outdated
Nebuvo laiko šią savaitę visai.. tai tik dabar siunčiu :)
private readonly ILogger<ExpenseController> _logger;

public ExpenseController (ILogger<ExpenseController> logger)
public IExpenseManager _expenseManager;

Copy link
Copy Markdown
Owner

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.

[HttpPost]
public void Post (ExpenseItem expenseItem)
{
_expenseManager.Insert(expenseItem);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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).

Comment thread Expense.cs
{
public DateTime ExpenseDate { get; set; }
public double Expenses { get; set; }
public int Expense_type_ID { get; set; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.

Comment thread Startup.cs
string connStr = Configuration.GetConnectionString("DatabaseConnectionString");

using var conn = new SqlConnection(connStr);
using var conn = new SqlConnection(Configuration.GetConnectionString("DatabaseConnectionString"));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Čia panaudok tą patį ConnectionToDB iš dependency injection

{
public class ConnectionToDB : IConnectionToDB
{
public IConfiguration Configuration { get; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Private šitie visur

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());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

čia _connection.Connect() sukuria objektą kurį reikia dispose'int.

{
public interface IConnectionToDB
{
IConfiguration Configuration { get; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants