Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/MyTimesheet/MyTimesheet/Controllers/SessionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyTimesheet.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Configuration;
using StackExchange.Redis;
using Microsoft.Extensions.Configuration;

namespace MyTimesheet.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SessionController : ControllerBase
{
private readonly SessionContext _db;
private readonly IConfiguration _configuration;
public SessionController(SessionContext context, IConfiguration config)
{
_db = context;
_configuration = config;
}

// GET api/values
[HttpGet]
public async Task<ActionResult<IEnumerable<SessionEntry>>> Get()
{
return await _db.Entries.ToListAsync();
}

// GET api/values/5
[HttpGet("{Session_Id}")]
public async Task<ActionResult<SessionEntry>> Get(int id)
{
return await _db.Entries.FindAsync(id);
}

// POST api/values
[HttpPost]
public async Task<String> Post([FromBody] SessionEntry value)
{
await _db.Entries.AddAsync(value);
await _db.SaveChangesAsync();

var cacheConnection = _configuration.GetValue<String>("CacheConnection").ToString();
var lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{

return ConnectionMultiplexer.Connect(cacheConnection);
});
IDatabase cache = lazyConnection.Value.GetDatabase();
await cache.SetAddAsync($"{value.Date} - {value.TimeStart} - {value.TimeEnd}", value.ToString());
var cacheItem = await cache.StringGetAsync($"{value.Date} - {value.TimeStart} - {value.TimeEnd}");

lazyConnection.Value.Dispose();
return cacheItem;
}


// PUT api/values/5
[HttpPut("{id}")]
public async Task Put(int id, [FromBody] SessionEntry value)
{
var entry = await _db.Entries.FindAsync(id);
entry = value;
await _db.SaveChangesAsync();
}

// DELETE api/values/5
[HttpDelete("{id}")]
public async Task Delete(int id)
{
var entry = await _db.Sessions.FindAsync(id);
_db.Sessions.Remove(entry);
await _db.SaveChangesAsync();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace MyTimesheet.Migrations
{
public partial class InitialMigrations : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Entries",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true),
Surname = table.Column<string>(nullable: true),
Client = table.Column<string>(nullable: true),
Project = table.Column<string>(nullable: true),
Date = table.Column<DateTime>(nullable: false),
TimeStart = table.Column<DateTime>(nullable: false),
TimeEnd = table.Column<DateTime>(nullable: false),
Duration = table.Column<int>(nullable: false),
Description = table.Column<string>(nullable: true),
Billable = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Entries", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Entries");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MyTimesheet.Models;

namespace MyTimesheet.Migrations
{
[DbContext(typeof(TimesheetContext))]
partial class TimesheetContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.1-servicing-10028")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("MyTimesheet.Models.TimesheetEntry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<bool>("Billable");

b.Property<string>("Client");

b.Property<DateTime>("Date");

b.Property<string>("Description");

b.Property<int>("Duration");

b.Property<string>("Name");

b.Property<string>("Project");

b.Property<string>("Surname");

b.Property<DateTime>("TimeEnd");

b.Property<DateTime>("TimeStart");

b.HasKey("Id");

b.ToTable("Entries");
});
#pragma warning restore 612, 618
}
}
}
1 change: 1 addition & 0 deletions src/MyTimesheet/MyTimesheet/MyTimesheet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
<PackageReference Include="StackExchange.Redis" Version="2.0.519" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MyTimesheet/MyTimesheet/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void ConfigureServices(IServiceCollection services)
c.SwaggerDoc("v1", new Info { Title = "My Timesheet API", Version = "v1" });
});

var connection = @"Server=sql101labs1793591179000.westeurope.cloudapp.azure.com;Database=sql101.#NAME.SURNAME;User Id=myUsername;Password=myPassword;";
var connection = @"Server=sql101labs1793591179000.westeurope.cloudapp.azure.com;Database=sql101.Zintle.Skosana;User Id=ZintleSkosana;Password=35983076zsS#;";
services.AddDbContext<TimesheetContext>
(options => options.UseSqlServer(connection));
}
Expand Down