yavsc/web/ApiControllers/FrontOfficeController.cs

191 lines
5.7 KiB
C#

10 years ago
using System;
using System.Collections.Generic;
10 years ago
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Profile;
using System.Web.Security;
using Yavsc.Formatters;
using Yavsc.Helpers;
using Yavsc.Model;
Refactoring: moving the Catalog manager and model into the Yavsc.Model.FrontOffice namespace * Web.config: * Catalog.xml: * MyClass.cs: * Note.cs: * Euro.cs: * Unit.cs: * Text.cs: * Link.cs: * Price.cs: * Label.cs: * Brand.cs: * Scalar.cs: * Option.cs: * Period.cs: * YavscModel.csproj: * Catalog.cs: * Service.cs: * Product.cs: * YavscClient.csproj: * CatalogManager.cs: * Currency.cs: * CheckBox.cs: * SaleForm.cs: * FormInput.cs: * CatalogProvider.cs: * TextInput.cs: * SelectItem.cs: * SalesCatalog.csproj: * FilesInput.cs: * FormElement.cs: * SelectInput.cs: * IValueProvider.cs: * StockStatus.cs: * RadioButton.cs: * Commande.cs: * ProductImage.cs: * WebCatalogExtensions.cs: * TemplateException.cs: * ProductCategory.cs: * PhysicalProduct.cs: * Note.cs: * Link.cs: * Text.cs: * Euro.cs: * Unit.cs: * WorkFlowManager.cs: * Brand.cs: * Label.cs: * Price.cs: * Scalar.cs: * FrontOfficeController.cs: * Period.cs: * Option.cs: * Product.cs: * Service.cs: * Catalog.cs: * SaleForm.cs: * Currency.cs: * CheckBox.cs: * TextInput.cs: * FrontOfficeApiController.cs: * FormInput.cs: * SelectItem.cs: * FilesInput.cs: * XmlCatalog.cs: * FormElement.cs: * SelectInput.cs: * RadioButton.cs: * StockStatus.cs: * ProductImage.cs: * CatalogHelper.cs: * CatalogManager.cs: * CatalogProvider.cs: * ProductCategory.cs: * PhysicalProduct.cs: * XmlCatalogProvider.cs: * CatalogProviderConfigurationElement.cs: * CatalogProvidersConfigurationSection.cs: * CatalogProvidersConfigurationCollection.cs: * CatalogProviderConfigurationElement.cs: * CatalogProvidersConfigurationSection.cs: * CatalogProvidersConfigurationCollection.cs: * CatalogHelper.cs:
10 years ago
using Yavsc.Model.FrontOffice;
using Yavsc.Model.RolesAndMembers;
using Yavsc.Model.WorkFlow;
using System.IO;
10 years ago
namespace Yavsc.ApiControllers
{
/// <summary>
/// Front office controller.
/// </summary>
10 years ago
public class FrontOfficeController : ApiController
{
/// <summary>
/// The wfmgr.
/// </summary>
protected WorkFlowManager wfmgr = null;
/// <summary>
/// Initialize the specified controllerContext.
/// </summary>
/// <param name="controllerContext">Controller context.</param>
protected override void Initialize (System.Web.Http.Controllers.HttpControllerContext controllerContext)
{
base.Initialize (controllerContext);
wfmgr = new WorkFlowManager ();
}
/// <summary>
/// Catalog this instance.
/// </summary>
[AcceptVerbs ("GET")]
10 years ago
public Catalog Catalog ()
{
Catalog c = CatalogManager.GetCatalog ();
return c;
10 years ago
}
/// <summary>
/// Gets the product categorie.
/// </summary>
/// <returns>The product categorie.</returns>
/// <param name="brandName">Brand name.</param>
/// <param name="prodCategorie">Prod categorie.</param>
[AcceptVerbs ("GET")]
10 years ago
public ProductCategory GetProductCategorie (string brandName, string prodCategorie)
{
return CatalogManager.GetCatalog ().GetBrand (brandName).GetProductCategory (prodCategorie);
10 years ago
}
/// <summary>
/// Authorization denied.
/// </summary>
public class AuthorizationDenied : HttpRequestException {
/// <summary>
/// Initializes a new instance of the <see cref="Yavsc.ApiControllers.FrontOfficeController+AuthorizationDenied"/> class.
/// </summary>
/// <param name="msg">Message.</param>
public AuthorizationDenied(string msg) : base(msg)
{
}
}
/// <summary>
/// Gets the estimate.
/// </summary>
/// <returns>The estimate.</returns>
/// <param name="id">Estimate Id.</param>
[Authorize]
[HttpGet]
public Estimate GetEstimate (long id)
{
Estimate est = wfmgr.ContentProvider.GetEstimate (id);
string username = Membership.GetUser ().UserName;
if (est.Client != username)
if (!Roles.IsUserInRole("Admin"))
if (!Roles.IsUserInRole("FrontOffice"))
throw new AuthorizationDenied (
string.Format (
"Auth denied to eid {1} for:{2}",
id, username));
return est;
}
/// <summary>
/// Gets the estim tex.
/// </summary>
/// <returns>The estim tex.</returns>
/// <param name="id">Estimate id.</param>
[AcceptVerbs ("GET")]
public HttpResponseMessage EstimateToTex (long id)
{
string texest = estimateToTex (id);
if (texest == null)
throw new InvalidOperationException (
"Not an estimate");
HttpResponseMessage result = new HttpResponseMessage () {
Content = new ObjectContent (typeof(string),
texest,
new SimpleFormatter ("text/x-tex"))
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue ("attachment") {
FileName = "estimate-" + id.ToString () + ".tex"
};
return result;
}
private string estimateToTex (long estimid)
{
Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim ();
Estimate e = wfmgr.GetEstimate (estimid);
tmpe.Session = new Dictionary<string,object> ();
tmpe.Session.Add ("estim", e);
Profile prpro = new Profile (ProfileBase.Create (e.Responsible));
10 years ago
if (!prpro.HasBankAccount)
throw new TemplateException ("NotBankable:" + e.Responsible);
if (!prpro.HasPostalAddress)
throw new TemplateException ("NoPostalAddress:" + e.Responsible);
Profile prcli = new Profile (ProfileBase.Create (e.Client));
if (!prcli.IsBillable)
throw new TemplateException ("NotBillable:" + e.Client);
tmpe.Session.Add ("from", prpro);
tmpe.Session.Add ("to", prcli);
tmpe.Session.Add ("efrom", Membership.GetUser (e.Responsible).Email);
tmpe.Session.Add ("efrom", Membership.GetUser (e.Client).Email);
tmpe.Init ();
return tmpe.TransformText ();
}
/// <summary>
/// Gets the estimate in pdf format from tex generation.
/// </summary>
/// <returns>The to pdf.</returns>
/// <param name="id">Estimid.</param>
[AcceptVerbs("GET")]
public HttpResponseMessage EstimateToPdf (long id)
{
string texest = null;
try {
texest = estimateToTex (id);
} catch (TemplateException ex) {
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
new ObjectContent (typeof(string),
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable,
LocalizedText.DocTemplateException
))
};
} catch (Exception ex) {
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
new ObjectContent (typeof(string),
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.InternalServerError,
LocalizedText.DocTemplateException))
};
}
if (texest == null)
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
new ObjectContent (typeof(string), "Not an estimation id:" + id,
new ErrorHtmlFormatter (HttpStatusCode.NotFound,
LocalizedText.Estimate_not_found))
};
var memPdf = new MemoryStream ();
HttpResponseMessage result = new HttpResponseMessage ();
new TexToPdfFormatter ().WriteToStream (
typeof(string), texest, memPdf,null);
memPdf.Position = 0;
var sr = new StreamReader(memPdf);
var str = sr.ReadToEnd();
result.Content = new StringContent (str);
TexToPdfFormatter.SetFileName (result.Content.Headers, "estimate-" + id.ToString ());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/x-tex");
return result;
}
10 years ago
10 years ago
}
}