#### 直接在檔案新增內建的gitignore檔案 dotnet new gitignore #### 創建mvc專案 dotnet new mvc -o stylish #### 在進行scaffold資料庫同步之前需要先安裝以下 dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.SqlServer #### 創建新的資料表是在dbcontext操作,創建新的資料表屬性是在.cs檔操作 #### 在terminal連線到資料庫,並且將資料表轉成檔案放在models底下 dotnet ef dbcontext scaffold "Server=database-1.cgnjpab792ga.ap-northeast-1.rds.amazonaws.com;Database=stylish;User Id=admin;Password=0975345817;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models --no-onconfiguring --use-database-names --no-pluralize --force dotnet ef dbcontext scaffold "Server=database-1.cgnjpab792ga.ap-northeast-1.rds.amazonaws.com;Database=Persnal_project;User Id=admin;Password=0975345817;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models --no-onconfiguring --use-database-names --no-pluralize --force * 資料庫連線 https://blog.talllkai.com/ASPNETCoreMVC/2023/04/18/DatabaseFirst #### 資料表關係 ##### 以下為多對一 * colors對product,每個顏色屬於一個產品 * sizes對product,每個尺寸數於一個產品 * images對product,每個圖片屬於一個產品 ##### 以下為一對多 * product對colors,一個產品有多個顏色選擇 * product對sizes,一個產品有多個尺寸選擇 * product對images,一個產品有多張照片 ##### 以下為多對多 一個variant可以有多個顏色、多個尺寸選擇 ### char/varchar/nvarchar https://ithelp.ithome.com.tw/articles/10213922 ![](https://hackmd.io/_uploads/r1aI7t0u3.png) ASP.NET MVC購物網站實作=> https://ithelp.ithome.com.tw/articles/10263113?sc=rss.iron 記得上傳vm的時候要改+:80 port,並且用sudo去run(如果只用dotnet run會出錯!) ![](https://hackmd.io/_uploads/SywM1SEFh.png) #### gitignore記得再加上這兩個檔案 ![](https://hackmd.io/_uploads/SJRWkrVF3.png) #### table都存在的情況下,可以用alert更改內部值 ``` ALTER TABLE dbo.variants ADD CONSTRAINT FK_variants_sizes FOREIGN KEY (size_id) REFERENCES dbo.sizes (size_id); ``` 4 #### 在資料庫新增自動生成的值可以參考以下 ``` SET IDENTITY_INSERT colors ON; INSERT INTO colors (color_id, product_id, color_name, color_code) VALUES (70, 62, 'white', 59426); SET IDENTITY_INSERT colors OFF; ``` #### 使用資料庫dbcontext的時候必須先在program進行初始化並連接接口 ``` builder.Services.AddDbContext<stylish.Models.stylishContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); }); //開頭必須先using Microsoft.EntityFrameworkCore; builder.Services.AddTransient<IProductRepository, ProductRepository>(); //這是另一個 ``` #### 如果上傳github遇到conflict可以參考 `git rm –cached cd /Users/yuchun/Documents/GitHub/Back-End-Class-91APP //先到當前目錄 git rm --cached -r students/irene/stylish/ //再進行` #### Bcrypt加密 分成header, payload, sinature Header:含 Token 的種類及產生簽章(signature)要使用的雜湊演算法 Payload:聲明(Claim)內容,放欲存放的資訊(User的基本資訊),不要將隱私資訊存放在 Payload Signature:base64編譯後的 Header、Payload 與密鑰(secret key)透過雜湊演算法所產生。 #### 安裝JWT `dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer` #### SQL設定欄位為自動生成 ``` CREATE TABLE Users( user_id INT IDENTITY(1, 1) PRIMARY KEY,); ``` #### 使用token步驟 week1part3 program.cs要先設定接口以及token設定 ``` builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(o => { o.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = builder.Configuration["Jwt:Issuer"], ValidAudience = builder.Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])), ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true }; }); builder.Services.AddAuthorization(); //// app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); // app.UseAuthorization(); // app.UseEndpoints(endpoints => // { endpoints.MapControllers(); }); ////以下為appsetting檔案 "Jwt": { "Issuer": "http://localhost:5108", "Audience": "http://localhost:5108", "Key": "99098741990987419909874199098741" }, ``` #### 新增臉書憑證 ``` <UserSecretsId>your-unique-user-secrets-id</UserSecretsId> //記得要先新增以上這行到.csproj dotnet user-secrets set "Authentication:Facebook:AppId" "100190313151639" --id your-user-secrets-id dotnet user-secrets set "Authentication:Facebook:AppSecret" "1ec2924e22f3ae5cf94a3ab43e34ee9a" --id your-user-secrets-id ``` #### 變數型態的影響(week1part4, fb登入) 變數如果都加上"?",則要注意判斷式的條件,如果沒加判斷是使用者就可以不填值,程式會發生錯誤 ![](https://hackmd.io/_uploads/rkIj-ghF2.png) #### async/await, Task任務 https://blog.opasschang.com/understand-csharp-asyn/ #### github #### week2part2,資料庫設計 儲存下列資料時需注意資料表的設計,必須分成兩張表,否則會出錯 ``` order: { shipping: "delivery", payment: "credit_card", subtotal: 1234, freight: 14, total: 1300, recipient: { name: "Luke", phone: "0987654321", email: "luke@gmail.com", address: "市政府站", time: "morning" }, list: [ { id: "62", name: "襪子", price: 100, color: { code: "55663", name: "black" }, size: "M", qty: 1 } ] } ``` 在發送請求給tappay時,除了基本的欄位格式,還需要加上x-api-pay,才能順利得到請求 在傳遞localstorage token的時候,要注意命名的一致性(例如:access_token) 在原網頁以及接收網頁都要特別注意!! access_token = access_token.Replace("Bearer ", "").Trim(); //特別注意!token會包含空格以及Bearer #### 生成檔案結構的tree指令 tree /Users/yuchun/Documents/GitHub/Back-End-Class-91APP/students/irene/stylish > "/Users/yuchun/Desktop/stylish review/tree_output.txt"