EnVisageOnline/Main-RMO/Source/EnVisage/Controllers/CapacityController.cs

143 lines
4.0 KiB
C#

using EnVisage.Code;
using EnVisage.Code.BLL;
using EnVisage.Code.DAL;
using EnVisage.Code.DAL.Mongo;
using EnVisage.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
namespace EnVisage.Controllers
{
public class CapacityController : BaseController
{
//
// GET: /Capacity/
public ActionResult Index()
{
Guid teamId = new Guid("e1e0e2fe-9043-4e15-ab82-f5eebe675717");
var mngr = new MongoMixManager(DbContext, User.Identity.GetID());
var mix = mngr.RetrieveWithChildren("55cb3773dbfb2b20e40ea513");
//mix.Teams[2].PlannedCapacity = new MixTeamCapacity[]
// {
// new MixTeamCapacity()
// {
// Id = Guid.NewGuid().ToString(),
// PlannedCapacityValues = new Dictionary<string, decimal>()
// }
// };
//mix.Teams[2].PlannedCapacity[0].PlannedCapacityValues.Add("1", 0);
//mix.Teams[2].PlannedCapacity[0].PlannedCapacityValues.Add("2", 40);
//mix.Teams[2].PlannedCapacity[0].PlannedCapacityValues.Add("3", 20);
//mix.Teams[2].PlannedCapacity[0].PlannedCapacityValues.Add("4", 25);
//mix.Teams[2].PlannedCapacity[0].PlannedCapacityValues.Add("5", 35);
//MongoDataContext.Mixes.ReplaceOneAsync(t => t.Id == mix.Id, mix).GetAwaiter().GetResult();
return View(teamId);
}
[HttpPost]
public JsonResult UploadFiles()
{
Guid userId = SecurityManager.GetUserPrincipal();
FileManager mngr = new FileManager(DbContext, userId, "~/App_Data");
var r = new List<AttachmentModel>();
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
AttachmentModel fileInfo = mngr.CreateTempFile(hpf);
//string savedFileName = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(hpf.FileName));
//hpf.SaveAs(savedFileName);
r.Add(fileInfo);
}
return Json(r, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult DeleteTempFile(Guid? id)
{
if (!id.HasValue || id.Value.Equals(Guid.Empty))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Guid userId = SecurityManager.GetUserPrincipal();
FileManager mngr = new FileManager(DbContext, userId, "~/App_Data");
try
{
mngr.DeleteTempFile(id.Value);
return Json("OK");
}
catch (Exception ex)
{
throw new BLLException(ex.Message, true);
}
}
[HttpGet]
public ActionResult GetTempFile(Guid? id)
{
if (!id.HasValue || id.Value.Equals(Guid.Empty))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Guid userId = SecurityManager.GetUserPrincipal();
FileManager mngr = new FileManager(DbContext, userId, "~/App_Data");
try
{
string contentType = "application/octet-stream";
FileDescriptor fileDescriptor = mngr.GetTempFileContent(id.Value);
if ((fileDescriptor.Meta != null) && !String.IsNullOrEmpty(fileDescriptor.Meta.ContentType))
contentType = fileDescriptor.Meta.ContentType;
BinaryReader rdr = new BinaryReader(fileDescriptor.Content);
byte[] contentAsBytes = rdr.ReadBytes((int)fileDescriptor.Content.Length);
FileContentResult result = new FileContentResult(contentAsBytes, contentType);
return result;
}
catch (Exception ex)
{
throw new BLLException(ex.Message, true);
}
}
[HttpPost]
public ActionResult ClearUserTempFolder()
{
Guid userId = SecurityManager.GetUserPrincipal();
FileManager mngr = new FileManager(DbContext, userId, "~/App_Data");
try
{
mngr.EmptyUserTempFolder();
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
catch (Exception ex)
{
throw new BLLException(ex.Message, true);
}
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
[HttpPost]
public ActionResult SaveProject()
{
return new EmptyResult();
}
}
}