yavsc/Yavsc/Controllers/EstimateController.cs

206 lines
6.5 KiB
C#
Raw Normal View History

2016-05-17 18:22:18 +02:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2016-12-01 17:34:29 +01:00
using System.Net.Mime;
2016-09-05 00:22:27 +02:00
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
2016-05-17 18:22:18 +02:00
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Extensions.OptionsModel;
using Yavsc.Helpers;
using Yavsc.Models;
2016-06-14 03:37:57 +02:00
using Yavsc.Models.Billing;
2016-09-05 00:22:27 +02:00
using Yavsc.Models.Booking;
2016-05-17 18:22:18 +02:00
namespace Yavsc.Controllers
{
2016-09-05 00:22:27 +02:00
[Authorize]
2016-05-17 18:22:18 +02:00
public class EstimateController : Controller
{
private ApplicationDbContext _context;
private SiteSettings _site;
public EstimateController(ApplicationDbContext context, IOptions<SiteSettings> siteSettings)
{
_context = context;
_site = siteSettings.Value;
}
// GET: Estimate
2016-09-05 00:22:27 +02:00
2016-05-17 18:22:18 +02:00
public IActionResult Index()
{
2016-09-05 00:22:27 +02:00
var uid = User.GetUserId();
2016-11-03 17:34:19 +01:00
return View(_context.Estimates.Include(e=>e.Query)
.Include(e=>e.Query.PerformerProfile)
.Include(e=>e.Query.PerformerProfile.Performer)
.Where(
2016-09-05 00:22:27 +02:00
e=>e.OwnerId == uid || e.ClientId == uid
).ToList());
2016-05-17 18:22:18 +02:00
}
// GET: Estimate/Details/5
public IActionResult Details(long? id)
{
2016-09-05 00:22:27 +02:00
var uid = User.GetUserId();
2016-05-17 18:22:18 +02:00
if (id == null)
{
return HttpNotFound();
}
2016-07-27 11:08:06 +02:00
Estimate estimate = _context.Estimates
2016-06-14 03:37:57 +02:00
.Include(e => e.Query)
.Include(e => e.Query.PerformerProfile)
.Include(e => e.Query.PerformerProfile.Performer)
2016-11-03 15:27:06 +01:00
.Include(e=> e.Bill)
2016-09-05 00:22:27 +02:00
.Where(
e=>e.OwnerId == uid || e.ClientId == uid
)
2016-05-17 18:22:18 +02:00
.Single(m => m.Id == id);
if (estimate == null)
{
return HttpNotFound();
}
return View(estimate);
}
// GET: Estimate/Create
2016-09-05 00:22:27 +02:00
[Authorize]
2016-05-17 18:22:18 +02:00
public IActionResult Create()
{
2016-09-05 00:22:27 +02:00
var uid = User.GetUserId();
IQueryable<BookQuery> queries = _context.BookQueries.Include(q=>q.Location).Where(bq=>bq.PerformerId == uid);
//.Select(bq=>new SelectListItem{ Text = bq.Client.UserName, Value = bq.Client.Id });
ViewBag.Clients = queries.Select(q=>q.Client).Distinct();
ViewBag.Queries = queries;
2016-05-17 18:22:18 +02:00
return View();
}
// POST: Estimate/Create
[HttpPost]
[ValidateAntiForgeryToken]
2016-07-27 11:08:06 +02:00
public IActionResult Create(Estimate estimate,
2016-05-17 18:22:18 +02:00
ICollection<IFormFile> newGraphics,
ICollection<IFormFile> newFiles
)
{
2016-09-05 00:22:27 +02:00
estimate.OwnerId = User.GetUserId();
2016-05-17 18:22:18 +02:00
if (ModelState.IsValid)
{
_context.Estimates
.Add(estimate);
_context.SaveChanges();
var perfomerProfile = _context.Performers
.Include(
perpr => perpr.Performer).FirstOrDefault(
2016-07-27 11:08:06 +02:00
x=>x.PerformerId == estimate.Query.PerformerId
2016-05-17 18:22:18 +02:00
);
2016-06-14 03:37:57 +02:00
var command = _context.BookQueries.FirstOrDefault(
2016-05-17 18:22:18 +02:00
cmd => cmd.Id == estimate.CommandId
);
2016-12-01 17:34:29 +01:00
var billsdir = Path.Combine(
_site.UserFiles.Bills,
2016-05-17 18:22:18 +02:00
perfomerProfile.Performer.UserName
);
foreach (var gr in newGraphics)
{
2016-12-01 17:34:29 +01:00
ContentDisposition contentDisposition = new ContentDisposition(gr.ContentDisposition);
2016-05-17 18:22:18 +02:00
gr.SaveAs(
Path.Combine(
Path.Combine(billsdir, estimate.Id.ToString()),
2016-12-01 17:34:29 +01:00
contentDisposition.FileName));
}
foreach (var formFile in newFiles)
{
ContentDisposition contentDisposition = new ContentDisposition(formFile.ContentDisposition);
formFile.SaveAs(
Path.Combine(
Path.Combine(billsdir, estimate.Id.ToString()),
contentDisposition.FileName));
2016-05-17 18:22:18 +02:00
}
return RedirectToAction("Index");
}
return View(estimate);
}
2016-09-05 00:22:27 +02:00
private void Save(ICollection<IFormFile> newGraphics,
2016-05-17 18:22:18 +02:00
ICollection<IFormFile> newFiles) {
}
// GET: Estimate/Edit/5
public IActionResult Edit(long? id)
{
if (id == null)
{
return HttpNotFound();
}
2016-09-05 00:22:27 +02:00
var uid = User.GetUserId();
2016-05-17 18:22:18 +02:00
2016-09-05 00:22:27 +02:00
Estimate estimate = _context.Estimates
.Where(e=>e.OwnerId==uid||e.ClientId==uid).Single(m => m.Id == id);
2016-05-17 18:22:18 +02:00
if (estimate == null)
{
return HttpNotFound();
}
2016-11-16 10:18:17 +01:00
ViewBag.Files = User.GetUserFiles(null);
2016-05-17 18:22:18 +02:00
return View(estimate);
}
// POST: Estimate/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
2016-07-27 11:08:06 +02:00
public IActionResult Edit(Estimate estimate)
2016-05-17 18:22:18 +02:00
{
2016-09-05 00:22:27 +02:00
var uid = User.GetUserId();
if (estimate.OwnerId!=uid&&estimate.ClientId!=uid
) return new HttpNotFoundResult();
2016-05-17 18:22:18 +02:00
if (ModelState.IsValid)
{
_context.Update(estimate);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(estimate);
}
// GET: Estimate/Delete/5
[ActionName("Delete")]
public IActionResult Delete(long? id)
{
if (id == null)
{
return HttpNotFound();
}
2016-09-05 00:22:27 +02:00
var uid = User.GetUserId();
2016-05-17 18:22:18 +02:00
2016-09-05 00:22:27 +02:00
Estimate estimate = _context.Estimates
.Where(e=>e.OwnerId==uid||e.ClientId==uid) .Single(m => m.Id == id);
2016-05-17 18:22:18 +02:00
if (estimate == null)
{
return HttpNotFound();
}
return View(estimate);
}
// POST: Estimate/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(long id)
{
2016-07-27 11:08:06 +02:00
Estimate estimate = _context.Estimates.Single(m => m.Id == id);
2016-05-17 18:22:18 +02:00
_context.Estimates.Remove(estimate);
_context.SaveChanges();
return RedirectToAction("Index");
}
}
}