using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using ASMS_BUSINESS_DOMAIN;
using ASMS_BUSINESS_DOMAIN.Extensions;
using ASMS_BUSINESS_DOMAIN.Global;
using ASMS_BUSINESS_DOMAIN.Repository;
using ASMS_MVC.GraphicConfig;
using ASMS_MVC.Models;
using ASMS_MVC.Global;
using ASMS_MVC.Helpers;
namespace ASMS_MVC.Controllers
{
[Authorize(Roles = Constants.ROLE_ADMIN)]
public class CBDPhotoController : BaseController
{
private IRepository<CBD_PHOTO> _cbdPhotoRepo;
public CBDPhotoController(IRepository<CBD_PHOTO> cbdPhotoRepo)
{
_cbdPhotoRepo = cbdPhotoRepo;
}
//
// GET: /CBDPhoto/
public ViewResult Index(int cbdid)
{
List<CBD_PHOTO> cbd_photos = _cbdPhotoRepo.Where(x => x.PARENT_ID == cbdid).ToList();
cbd_photos.Sort(new CBD_PHOTO_COMPARER());
ViewBag.cbdid = cbdid;
ViewBag.ELEM_TXT_PHOTO_TITLE = Constants.ELEM_TXT_PHOTO_TITLE;
ViewBag.MAX_THUMBNAIL_WIDTH = Constants.MAX_THUMBNAIL_WIDTH;
return View(Constants.PARTIALVIEW_INDEX, cbd_photos);
}
/// <summary>
/// Returns an Image using the raw THUMBNAIL data from the DB
/// </summary>
public FileContentResult GetThumbnail(int cbdPhotoId)
{
return new FileContentResult(Utils.GetThumbnail(cbdPhotoId, _cbdPhotoRepo), "image/jpeg");
}
/// <summary>
/// Returns an Image file of Medium quality for display in the 'Edit Photo' view of the CBD Site Admin screens
/// </summary>
public FileContentResult GetEditImage(int cbdPhotoId)
{
byte[] result = Utils.GetImageResult(cbdPhotoId, Constants.MAX_EDIT_IMAGE_WIDTH, ImageQuality.Medium, ImageFormat.Jpeg, _cbdPhotoRepo);
return new FileContentResult(result, "image/jpeg");
}
/// <summary>
/// Creates a High Quality Image from the raw data in the DB
/// </summary>
public FileContentResult GetFullResImage(int cbdPhotoId)
{
byte[] result = Utils.GetImageResult(cbdPhotoId, null, ImageQuality.High, ImageFormat.Jpeg, _cbdPhotoRepo);
return new FileContentResult(result, "image/jpeg");
}
public ViewResult View(int cbdPhotoId)
{
CBD_PHOTO cbd_photo = _cbdPhotoRepo.Single(c => c.PHOTO_ID == cbdPhotoId);
return View(Constants.PARTIALVIEW_VIEW, cbd_photo);
}
//
// GET: /CBDPhoto/Edit/5
public ActionResult Edit(int cbdPhotoId)
{
CBD_PHOTO cbd_photo = _cbdPhotoRepo.Single(c => c.PHOTO_ID == cbdPhotoId);
ViewBag.CBD_SITE_ID = cbd_photo.PARENT_ID;
return View(Constants.PARTIALVIEW_EDIT, cbd_photo);
}
//
// POST: /CBDPhoto/Edit/5
[HttpPost]
public ActionResult Edit(CBD_PHOTO cbd_photo)
{
if (ModelState.IsValid)
{
// NOTE: We don't get the image data serialized because the client doesn't have the full resolution cbdPhoto.
// EF complains if we try and update the cbdPhoto without included the cbdPhoto data, since it is a non-nullable field.
// This problem is solved by using the Table Split method of table mapping: splitting the PHOTO_DATA into it's own entity
_cbdPhotoRepo.Add(cbd_photo);
_cbdPhotoRepo.Context.ObjectStateManager.ChangeObjectState(cbd_photo, EntityState.Modified);
_cbdPhotoRepo.SaveChanges();
// Return the full cbdPhoto entity, serialized into a JSON object so we can update the View
return Json(new { CBD_PHOTO_ID = cbd_photo.PHOTO_ID, cbd_photo.TITLE });
}
ViewBag.CBD_SITE_ID = cbd_photo.PARENT_ID;
return View(Constants.PARTIALVIEW_EDIT, cbd_photo);
}
//
// GET: /CBDPhoto/Delete/5
public ActionResult Delete(int cbdPhotoId)
{
// Eager load the photo data
CBD_PHOTO cbd_photo = _cbdPhotoRepo.Where(c => c.PHOTO_ID == cbdPhotoId).Include(p => p.PHOTO_DATA).Include(p => p.THUMBNAIL_DATA).Single();
if (cbd_photo == null)
{
//Return record not found error
return View(Constants.PARTIALVIEW_DELETE, cbd_photo);
}
else
{
_cbdPhotoRepo.Delete(cbd_photo);
_cbdPhotoRepo.SaveChanges();
// Serialize an annoymous type containing the id of photo we just deleted so the client script knows which div wrapper to remove
return Json(new { CBD_PHOTO_ID = cbd_photo.PHOTO_ID }, JsonRequestBehavior.AllowGet);
}
}
protected override void Dispose(bool disposing)
{
_cbdPhotoRepo.Dispose();
base.Dispose(disposing);
}
}
}