256 lines
9.9 KiB
C#
256 lines
9.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Web.Mvc;
|
|
using EnVisage.App_Start;
|
|
using EnVisage.Code;
|
|
using EnVisage.Code.BLL;
|
|
using EnVisage.Models;
|
|
using jQuery.DataTables.Mvc;
|
|
using System.Collections.ObjectModel;
|
|
using EnVisage.Code.Validation;
|
|
|
|
namespace EnVisage.Controllers
|
|
{
|
|
[Authorize]
|
|
public class WorkWeekController : BaseController
|
|
{
|
|
/// <summary>
|
|
/// GET: /Types/
|
|
/// </summary>
|
|
/// <returns>Empty view</returns>
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.WorkWeeks, level = AccessLevel.Read)]
|
|
public ActionResult Index()
|
|
{
|
|
if (!SecurityManager.CheckSecurityObjectPermission(Areas.WorkWeeks, AccessLevel.Read))
|
|
return Redirect("/");
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns JSON Types list with filters and sort for jQuery DataTables
|
|
/// </summary>
|
|
[HttpPost]
|
|
[AreaSecurity(area = Areas.WorkWeeks, level = AccessLevel.Read)]
|
|
public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel)
|
|
{
|
|
int totalRecordCount;
|
|
int searchRecordCount;
|
|
|
|
|
|
var goals = GetWorkWeeks(startIndex: jQueryDataTablesModel.iDisplayStart,
|
|
pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(),
|
|
totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch,
|
|
filters: jQueryDataTablesModel.sSearch_);
|
|
|
|
return this.DataTablesJson(items: goals,
|
|
totalRecords: totalRecordCount,
|
|
totalDisplayRecords: searchRecordCount,
|
|
sEcho: jQueryDataTablesModel.sEcho);
|
|
|
|
|
|
}
|
|
|
|
private IEnumerable<WorkWeekModel> GetWorkWeeks(int startIndex,
|
|
int pageSize,
|
|
IEnumerable<SortedColumn> sortedColumns,
|
|
out int totalRecordCount,
|
|
out int searchRecordCount,
|
|
string searchString,
|
|
ReadOnlyCollection<string> filters)
|
|
{
|
|
|
|
var query = from c in DbContext.WorkWeeks
|
|
select new WorkWeekModel()
|
|
{
|
|
Id = c.Id,
|
|
Name = c.Name,
|
|
Monday = c.Monday,
|
|
Tuesday = c.Tuesday,
|
|
Wednesday = c.Wednesday,
|
|
Thursday = c.Thursday,
|
|
Friday = c.Friday,
|
|
Saturday = c.Saturday,
|
|
Sunday = c.Sunday,
|
|
IsDefault = c.IsDefault,
|
|
IsSystem = c.IsSystem,
|
|
CanBeDeleted = !c.IsDefault && c.PeopleResources.Count == 0
|
|
};
|
|
|
|
var filteredQuery = query;
|
|
//filter
|
|
if (!string.IsNullOrWhiteSpace(searchString))
|
|
{
|
|
filteredQuery = filteredQuery.Where(c => c.Name.ToLower().Contains(searchString.ToLower()));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(filters[0]))
|
|
filteredQuery = filteredQuery.Where(c => c.Name.ToLower().Equals(filters[0].ToLower()));
|
|
|
|
//sort
|
|
foreach (var sortedColumn in sortedColumns)
|
|
{
|
|
switch (sortedColumn.PropertyName)
|
|
{
|
|
case "Id":
|
|
if (sortedColumn.Direction == SortingDirection.Ascending)
|
|
filteredQuery = filteredQuery.OrderBy(c => c.Id);
|
|
else
|
|
filteredQuery = filteredQuery.OrderByDescending(c => c.Id);
|
|
break;
|
|
default:
|
|
if (sortedColumn.Direction == SortingDirection.Ascending)
|
|
filteredQuery = filteredQuery.OrderBy(c => c.Name);
|
|
else
|
|
filteredQuery = filteredQuery.OrderByDescending(c => c.Name);
|
|
break;
|
|
}
|
|
}
|
|
|
|
totalRecordCount = query.Count();
|
|
searchRecordCount = filteredQuery.Count();
|
|
var result = filteredQuery.Skip(startIndex).Take(pageSize).ToList();
|
|
result.ForEach(x => x.DaysOfWeek = WorkWeekModel.getDaysOfWeek(x, string.Empty, false));
|
|
return result;
|
|
}
|
|
|
|
// GET: /Type/Edit/5
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.WorkWeeks, level = AccessLevel.Write)]
|
|
public ActionResult Edit(Guid? id)
|
|
{
|
|
try
|
|
{
|
|
var manager = new WorkWeekManager(DbContext);
|
|
var model = (WorkWeekModel)manager.Load(id) ?? new WorkWeekModel();
|
|
|
|
return PartialView("_editWorkWeek", model);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
[HttpPost]
|
|
[AreaSecurity(area = Areas.WorkWeeks, level = AccessLevel.Write)]
|
|
public ActionResult Activate(Guid id)
|
|
{
|
|
if (id == Guid.Empty)
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
try
|
|
{
|
|
var manager = new WorkWeekManager(DbContext);
|
|
manager.Activate(id);
|
|
DbContext.SaveChanges();
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.OK);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
// POST: /Type/Edit/5
|
|
// Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные
|
|
// сведения см. в статье http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAjax]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurity(area = Areas.WorkWeeks, level = AccessLevel.Write)]
|
|
public ActionResult Edit(WorkWeekModel model)
|
|
{
|
|
if (ContentLocker.IsLock("WorkWeek", model.Id.ToString(), User.Identity.GetUserName()))
|
|
{
|
|
ModelState.AddModelError(string.Empty, "This work week is currently being updated by another user. Please attempt your edit again later.");
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
|
|
model.TrimStringProperties();
|
|
|
|
try
|
|
{
|
|
var manager = new WorkWeekManager(DbContext);
|
|
manager.Save(model);
|
|
DbContext.SaveChanges();
|
|
|
|
ContentLocker.RemoveLock("WorkWeek", model.Id.ToString(), User.Identity.GetUserName());
|
|
|
|
return new SuccessJsonResult();
|
|
}
|
|
catch (BLLException blEx) // handle any system specific error
|
|
{
|
|
// display error message if required
|
|
if (blEx.DisplayError)
|
|
ModelState.AddModelError(string.Empty, blEx.Message);
|
|
else // if display not requried then display modal form with general error message
|
|
{
|
|
LogException(blEx);
|
|
ModelState.AddModelError(string.Empty, "Cannot save work week. Try again later.");
|
|
}
|
|
}
|
|
catch (Exception exception) // handle any unexpected error
|
|
{
|
|
LogException(exception);
|
|
ModelState.AddModelError(string.Empty, "Cannot save work week. Try again later.");
|
|
}
|
|
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
|
|
// POST: /Type/Delete/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurity(area = Areas.WorkWeeks, level = AccessLevel.Write)]
|
|
public ActionResult Delete(Guid deleteWorkWeekId)
|
|
{
|
|
if (ContentLocker.IsLock("WorkWeek", deleteWorkWeekId.ToString(), User.Identity.GetUserName()))
|
|
{
|
|
ModelState.AddModelError(string.Empty, "This work week is currently being updated by another user. Please attempt your edit again later.");
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
var workWeek = DbContext.WorkWeeks.Where(x => x.Id == deleteWorkWeekId).FirstOrDefault();
|
|
if (workWeek == null)
|
|
throw new InvalidOperationException(string.Format("Work week {0} cannot be deleted, because it does not exist", deleteWorkWeekId));
|
|
|
|
DbContext.WorkWeeks.Remove(workWeek);
|
|
DbContext.SaveChanges();
|
|
|
|
ContentLocker.RemoveLock("WorkWeek", workWeek.Id.ToString(), User.Identity.GetUserName());
|
|
|
|
return new SuccessJsonResult();
|
|
}
|
|
catch (BLLException blEx) // handle any system specific error
|
|
{
|
|
// display error message if required
|
|
if (blEx.DisplayError)
|
|
ModelState.AddModelError(string.Empty, blEx.Message);
|
|
else // if display not requried then display modal form with general error message
|
|
{
|
|
LogException(blEx);
|
|
ModelState.AddModelError(string.Empty, "Cannot delete work week. Try again later.");
|
|
}
|
|
}
|
|
catch (Exception exception) // handle any unexpected error
|
|
{
|
|
LogException(exception);
|
|
ModelState.AddModelError(string.Empty, "Cannot delete work week. Try again later.");
|
|
}
|
|
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
}
|
|
} |