PS D:\workspace\Web\Vue3> npm init vue@latest
Need to install the following packages:
create-vue@3.5.0
Ok to proceed? (y) y
Vue.js - The Progressive JavaScript Framework
√ Project name: ... vue-project
√ Add TypeScript? ... No / Yes
√ Add JSX Support? ... No / Yes
√ Add Vue Router for Single Page Application development? ... No / Yes
√ Add Pinia for state management? ... No / Yes
√ Add Vitest for Unit Testing? ... No / Yes
√ Add an End-to-End Testing Solution? » No
√ Add ESLint for code quality? ... No / Yes
Scaffolding project in D:\workspace\Web\Vue3\vue-project...
Done. Now run:
cd vue-project
npm install
npm run dev
PS D:\workspace\Web\Vue3> cd vue-project
PS D:\workspace\Web\Vue3\vue-project> npm install
npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
added 32 packages, and audited 33 packages in 8s
4 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
PS D:\workspace\Web\Vue3\vue-project> npm run dev
> vue-project@0.0.0 dev
> vite
VITE v4.0.4 ready in 375 ms
➜ Local: http://127.0.0.1:5173/
➜ Network: use --host to expose
➜ press h to show help
('h' キーを押す)
Shortcuts
press r to restart the server
press u to show server url
press o to open in browser
press q to quit
('q' キーを押す)
PS D:\workspace\Web\Vue3\vue-project>
// dotnet add package MySql.EntityFrameworkCore が必要。
using Microsoft.EntityFrameworkCore;
namespace WebBlazerServer.Data;
/// <summary>
/// Medias テーブルのモデル
/// </summary>
public class MediasModel
{
private MediasContext _mysqldb;
// コンストラクタ
public MediasModel()
{
_mysqldb = new MediasContext();
}
// すべての項目を取得する。
public List<Medias> QueryAll()
{
var result = from item in _mysqldb.Medias select item;
return result.ToList<Medias>();
}
// id で指定した 1 行を取得する。
public Medias GetRow(int id)
{
var result = from item in _mysqldb.Medias where item.id == id select item;
return result.ToArray<Medias>()[0];
}
// id で指定したメディア名を返す。
public string? GetName(int id)
{
var result = from item in _mysqldb.Medias where item.id == id select item.name;
return result.ToArray<string>()[0];
}
// 条件に基づいて項目を取得する。
public List<Medias> Query(Func<Medias, bool> predicate)
{
//var result = from item in _mysqldb.Medias where predicate(item) select item;
var result = _mysqldb.Medias.Where(predicate).ToList<Medias>();
return result;
}
// 新規追加
public void Insert(Medias item)
{
_mysqldb.Add(item);
_mysqldb.SaveChanges();
}
// 更新
public void Update(int id, Medias item)
{
var data = this.GetRow(id);
if (item.name != null)
data.name = item.name;
if (item.type != null)
data.type = item.type;
if (item.format != null)
data.format = item.format;
if (item.size != null)
data.size = item.size;
if (item.fixed_on != null)
data.fixed_on = item.fixed_on;
if (item.info != null)
data.info = item.info;
_mysqldb.Update(data);
_mysqldb.SaveChanges();
}
// 削除
public void Delete(int id)
{
var item = this.GetRow(id);
_mysqldb.Remove(item);
_mysqldb.SaveChanges();
}
}
// Medias テーブルの定義
public class Medias
{
public Medias()
{
}
public int? id { get; set; }
public string? name { get; set; }
public string? type { get; set; }
public string? format { get; set; }
public string? size { get; set; }
public string? fixed_on { get; set; }
public string? info { get; set; }
public DateTime? date { get; set; }
}
// Medias の DbContext
public class MediasContext : DbContext
{
public DbSet<Medias>? Medias { get; set; }
// DB 接続設定
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL("Server=localhost;Database=user;Uid=user;Pwd=ust62kjy");
}
}
MediasModelService.cs
namespace WebBlazerServer.Data;
public class MediasModelService
{
public Task<List<Medias> > QueryMediasAllAsync()
{
var medias = new MediasModel();
return Task.FromResult(medias.QueryAll());
}
}
// 拡張された switch 文
Console.WriteLine("** 拡張された switch 文 **");
object x = 1000;
// 型だけを判別
switch (x)
{
case string:
Console.WriteLine("x is string");
break;
case int:
Console.WriteLine("x is int");
break;
default:
Console.WriteLine("?");
break;
}
// 型判別と型変換
switch (x)
{
case int n:
Console.WriteLine("x is int {0}", n);
break;
default:
Console.WriteLine("?");
break;
}
// 型と範囲を判別
switch (x)
{
case int n when n > 0:
Console.WriteLine("x is int and plus {0}", n);
break;
default:
Console.WriteLine("?");
break;
}
// 複数条件で型と範囲を判別 (when)
object y = -1.5;
switch (y)
{
case int n when n > 0:
Console.WriteLine("y is int and plus {0}", n);
break;
case double a when a > 0.0:
Console.WriteLine("y is double and plus {0}", a);
break;
case double a when a < 0.0:
Console.WriteLine("y is double and minus {0}", a);
break;
default:
Console.WriteLine("?");
break;
}