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
203 changes: 203 additions & 0 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using QRCoder;

namespace dontKillBill.Controllers
{
public class HomeController : Controller
{

private readonly ApplicationDbContext _context;
private readonly IHostingEnvironment _env;



public HomeController(ApplicationDbContext context, IHostingEnvironment env)
{
_context = context;
_env = env;
}


public IActionResult Index()
{
//staci aby to zbehlo raz
// FindFreeIntervals.Find(_context);

return View();

}
public IActionResult SelectInterval(MenuViewModel model)
{
if (!ModelState.IsValid)
{
return Content("Neplatny vstup", "text/html");
}

var vouchers = _context.Vouchers.OrderBy(m => m.VoucherId).Skip(model.StartPoint-1).Take(model.EndPoint-model.StartPoint).ToList();

return View(vouchers);
}

public IActionResult NewVoucher(MenuViewModel viewModel)
{
if (!ModelState.IsValid)
{
return Content("Neplatny vstup", "text/html");
}

GenerateVouchers(viewModel.Count);

return View();
}


public IActionResult QrCode(MenuViewModel viewModel)
{
if (!ModelState.IsValid)
{
return Content("Neplatny vstup", "text/html");
}

string path=string.Empty;
string exist = Exist(viewModel.ToQr);

if(exist == viewModel.ToQr.ToString())
{
path = GenerateQrCode(viewModel.ToQr);
}

ViewData["exist"] = exist;
ViewData["qrCode"] = path;
return View();
}

#region Generate new vouchers

private void GenerateVouchers(int count)
{
//ak nieje co vytvarat
if (count == 0)
return;


var intervals = _context.FreeIntervals.Where(u => u.Used == false);

//ak uz nepotrebujem generovat podla intervalov
if (!intervals.Any())
{
GenerateVouchersFromEnd(count);
}

//generovanie z intervalov
else
{
intervals = intervals.OrderBy(s => s.StartOfInterval).Take(count);
GenerateVouchersFromInterval(intervals, count);
}

_context.SaveChanges();
}


private void GenerateVouchersFromEnd(int count)
{
var last = _context.Vouchers.OrderByDescending(v => v.VoucherId).FirstOrDefault();
int start = last.VoucherId+1;
int end = last.VoucherId+count;

for (int i = start; i < end; i++)
{
var voucher = new Vouchers { VoucherId = i };
_context.Add(voucher);
}
}

private void GenerateVouchersFromInterval(IQueryable<FreeIntervals> intervals, int count)
{
int generated = 0;

foreach (var interval in intervals)
{


int start = interval.StartOfInterval;
int end = interval.EndOfInterval;

for (int i = start; i < end; i++)
{
generated++;
var voucher = new Vouchers { VoucherId = i };
_context.Add(voucher);

if (generated == count)
{
interval.StartOfInterval = i + 1;

return;
}
}
interval.Used = true;
}

//ak sa to dostane az sem treba dogenerovat vouchre aj za vsetkymi intervalmi
GenerateVouchersFromEnd(count - generated);
}
#endregion

#region QrCode

private string Exist(int id)
{
var result = _context.Vouchers.Find(id);

return result == null ? "Takyto voucher zatial neexistuje" : id.ToString();
}

private string GenerateQrCode(int voucherId)
{
string id = voucherId.ToString();
var webRoot = _env.WebRootPath;

//ak Qr code este nebol vytvoreny, vytvor ho
if(!FindQrCode(voucherId, webRoot))
{
var fullPath = Path.Combine(webRoot, "QrCodes", "voucher_" + id + ".png");

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(id, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);

qrCodeImage.Save(fullPath, ImageFormat.Png);
}

return Path.Combine("/QrCodes", "voucher_" + id + ".png");
}

private bool FindQrCode(int voucherId,string webRoot)
{
var _fileProvider = new PhysicalFileProvider(webRoot);

var contents = _fileProvider.GetDirectoryContents("/QrCodes");

var result = contents.ToList().Find(n => n.Name == "voucher_" + voucherId.ToString() + ".png");

if (result == null)
return false;

return true;
}
#endregion
}

}
11 changes: 11 additions & 0 deletions src/INFO.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-mail: martin.trojan.372@gmail.com

-ked som to chcel len pushnut tak

$ git push origin Trojan_Martin
Warning: Permanently added the RSA host key for IP address '140.82.118.3' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
51 changes: 51 additions & 0 deletions src/Migrations/ApplicationDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using dontKillBill;

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

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

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

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

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

b.HasKey("Id");

b.ToTable("FreeIntervals");
});

modelBuilder.Entity("dontKillBill.Vouchers", b =>
{
b.Property<int>("VoucherId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.HasKey("VoucherId");

b.ToTable("Vouchers");
});
#pragma warning restore 612, 618
}
}
}
25 changes: 25 additions & 0 deletions src/Models/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace dontKillBill
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{

}

public DbSet<Vouchers> Vouchers { get; set; }

public DbSet<FreeIntervals> FreeIntervals { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
24 changes: 24 additions & 0 deletions src/Models/FreeIntervals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace dontKillBill
{
public class FreeIntervals
{

[Key]
public int Id { get; set; }

public int StartOfInterval { get; set; }

public int EndOfInterval { get; set; }

public bool Used { get; set; }



}
}
40 changes: 40 additions & 0 deletions src/Models/ViewModels/MenuViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace dontKillBill
{
public class MenuViewModel
{
/// <summary>
/// start of selected interval
/// </summary>
[Range(0,int.MaxValue)]
[Display(Name ="Zaciatok intervalu")]
public int StartPoint { get; set; }

/// <summary>
/// end of selected interval
/// </summary>
[Range(0, int.MaxValue, ErrorMessage = "Hodnota musi byt kladna")]
[Display(Name = "Koniec intervalu")]
public int EndPoint { get; set; }

/// <summary>
/// Number of vouchers to be generated
/// </summary>
[Range(0, int.MaxValue,ErrorMessage ="Hodnota musi byt kladna")]
[Display(Name = "Pocet voucherov ktore maju byt vygenerovane")]
public int Count { get; set; }

/// <summary>
/// voucher id to be generated to qr code
/// </summary>
[Range(0, int.MaxValue, ErrorMessage = "Hodnota musi byt kladna")]
[Display(Name = "Pocet voucherov ktore maju byt vygenerovane")]
public int ToQr { get; set; }

}
}
14 changes: 14 additions & 0 deletions src/Models/Vouchers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace dontKillBill
{
public class Vouchers
{
[Key]
public int VoucherId { get; set; }
}
}
Loading