Files
yavsc/NpgsqlContentProvider/NpgsqlContentProvider.cs
Paul Schneider d999ae5a8e Thème clair opé, WIP circles
* animate.css: animations css

* links.css: liens avec le fond blanc qui se déroule

* links.css: WIP liens thème sombre

* GitHub.ico: un petit merci à GitHub

* live-concert-fg.png: image de plan rapproché : les fans

* live-concert-scene.jpg: Image de fond : la scene

* splash-image-2.jpg: image de fond : photo colorée, public de
  concert, vient de totemproduction.fr

* links.css: ficher de définitions globales du style des bouttons
  (encore inutilisé)

* yavsc.user.js: WIP implémente le widget "utilisateur", permettant
l'affichage des liens rapides pour tout nom d'ulitisateur, le cas
  échéant, vers l'ajout à un cercle,
vers son blog, vers des methodes choisies d'administateur, vers ses
  réseaux sociaux, voir plus.

* About.aspx: Implémente une page "à propos"

* Test.aspx: Page de test: désolé pour le bruit, elle n'est compilée
  qu'en mode `Debug` (elle n'est pas accessible en production)

* NpgsqlBlogProvider.cs: Corrige la méthode pour enlever un tag à un
  article ...

* NpgsqlBlogProvider.csproj: .

* NpgsqlCircleProvider.cs: liste les cercles auquels appartient un
  utilisateur.

* BasketController.cs:
* NpgsqlContentProvider.cs: Les commandes sont fortement typée.

* README.md: mise à jour des priorités

* style.css: réécriture du thème clair

* style.css: * implémente un classe css `clickme` pour attirer l'oeil
sur le boutton de validation après la modification d'un champ de
  formulaire.
* enlève tous les coins arrondis
* modifie la chasse de caractères (une seule utilisée pour le moment)
*

* style.css: * integre la référence à la feuille de style des
  [animations](https://daneden.github.io/animate.css/)
* corrige des marges, alignements
* supprime ce qui reste de coin arrondi
* néttoye les définitions obsolètes
* définit la classe `username`
* corrige le survol à la suppression de tag

* FrontOfficeController.cs: renomme des methodes, refabriqué pour
  s'adapter aux changements de l'API du workflow,
WIP propose les cercles utilisateurs pour l'ajout des profiles
  préstataires aux cercles.

* HomeController.cs: implémente une page "à propos"

* T.cs: re-autorise le titre vide de page, au cas où ... car ce n'est
  pas un véritable
défaut fonctionnel.

* YavscHelpers.cs: Implemente un outil de presentation des liens
  action traduits. Tous les liens action utilisent maintenant cette
  nouvelle methode,
au profile de celle du framework `HtmlHelpers.ActionLink`

* App.master: corrige l'usage du contrôle utilisateur des cotes dans
  le cadre des cotes de billet.

* AppAdmin.master:
* Index.aspx:
* Service.aspx:
* ProductCategory.aspx: traduction des liens action

* NoLogin.master: * traduction des liens action
* suppression des liens de remerciement dans le bas de page, cette
  information a été déplacée dans la page `/Home/Credits`

* to-markdown.js: transforme maintenant les noeuds Html `div` en
  paragraphes Markdown.

* yavsc.hidefieldset.js: Le script règle la forme de la souris au
  survol du bouton,
le cas échéant.

* yavsc.js: nouvelle fonction javascript pour logger un objet
  arbitraire.

* yavsc.rate.js: Fixe la possibilité de mettre des cotes très basses
  ou très hautes (reste encore un mieux à faire),
Affiche au survol des descriptions optionnelles de la cote.

* Title.aspx:
* UserPost.aspx:
* Edit.aspx:
* Index.aspx:
* Brand.aspx:
* Index.aspx:
* PostActions.ascx:
* TitleNotFound.aspx:
* Booking.aspx:
* Command.aspx:
* ChangePasswordSuccess.aspx: Traduction des liens action

* Login.aspx: * Traduction des liens actionTraduction des liens action

* Profile.aspx: * Traduction des liens actionTraduction des liens
  action
* utilisation de la classe css `mayhide` qui autorise maintenant un
  champ d'entrées de formulaire à être plié/déplié au click sur sa
  légende.

* Admin.aspx: Traduction des liens actionTraduction des liens
  actionTraduction des liens actionTraduction des liens
  actionTraduction des liens actionTraduction des liens action

* Backups.aspx:
* RoleList.aspx:
* UserList.aspx:
* Index.aspx: Traduction des liens actionTraduction des liens action

* Edit.aspx: * refabrication de la structure Html
* Traduction des liens actionTraduction des liens action

* Index.aspx: Simlpification des apperçus de blog

* TagControl.ascx: refabrication pour simplifier l'implémentation du
  contrôle utilisateur des tags

* UserPosts.aspx: Traduction des liens action, et simplification du
  listing de blog

* Basket.aspx: Affiche les articles du panier de commandes avec leur
  vue
dédiée à leur type (les commande sont maintenant fortement typées)

* Catalog.aspx: Traduction des liens action, correctin du lien vers le
  produit
(cassé depuis une vielle refabrication des routes)

* DoAnEstimate.aspx: refabrication de la vue de création du devis:
  renommée depuis `Estimate`

* Performer.ascx: * un message supplementaire à l'absence de
  compétence affichée par un préstataire
* des guillemets autour du commentaire du préstataire sur sa
  compétence
* ajout du préstataire au cercles

* Performers.aspx: Décrit en détail les informations sur la
  disponibilité d'un préstataire,
à la date demandée.

* YourEstimates.aspx: renomage de cette page, pour préciser sa
  fonction : Vos devis.

* Contact.aspx: structure Html de la page de contact

* Credits.aspx: Ajoute les remerciements aux communauté du libre

* Index.aspx: ne fait rien

* Web.config: * import de `System.Transactions`
* mise à jour des credits libres

* WebDeploy.targets: utilise les séparateurs de chemin vers ficher à
  la POSIX, ne change rien pour ma config ... (TODO tester sous
  WoinDose)

* Yavsc.csproj: déploie des pages des scripts Javascript et des images
  et feuilles de style en plus.

* CircleManager.cs: implémente les methodes utilisées à la
  construction du contrôle utilisateur.

* CircleProvider.cs: * doc xml
* extension de interface

* Commande.cs: l'objet `Command` devient abstrait, pour exister (être
  instanciée), une commande doit maintenant hériter de cet objet
  abstrait.

* LocalizedText.resx:
* LocalizedText.fr.resx:
* LocalizedText.Designer.cs:
* LocalizedText.fr.Designer.cs: traductions

* IContentProvider.cs: doc xml

* SiteSkills.aspx: renomage de cette page, pour préciser sa fonction :
  les compétences sur ce site.
2015-12-09 13:26:02 +01:00

752 lines
23 KiB
C#

using System;
using Npgsql;
using NpgsqlTypes;
using System.Configuration;
using System.Collections.Specialized;
using Yavsc.Model.WorkFlow;
using System.Configuration.Provider;
using System.Collections.Generic;
using Yavsc.Model.FrontOffice;
using System.Web.Security;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Yavsc.Model.FrontOffice.Catalog;
namespace Yavsc
{
/// <summary>
/// Npgsql content provider.
/// </summary>
public class NpgsqlContentProvider: ProviderBase, IContentProvider
{
/// <summary>
/// Finds the performer.
/// </summary>
/// <returns>The performer.</returns>
/// <param name="MEACode">MEA code.</param>
public PerformerProfile[] FindPerformer (string MEACode)
{
var result = new List<PerformerProfile> ();
using (NpgsqlConnection cnx = CreateConnection ()) {
cnx.Open ();
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText = @"SELECT d.uniqueid, u.username, u.email, d.rate
FROM profiledata d, profiles p, users u
WHERE u.username = p.username
AND u.applicationname = p.applicationname
AND p.uniqueid = d.uniqueid
AND u.isapproved = TRUE
AND u.islockedout = FALSE
AND d.meacode = :mea
AND u.applicationname = :app
ORDER BY u.username ";
cmd.Parameters.AddWithValue ("mea", NpgsqlTypes.NpgsqlDbType.Varchar, MEACode);
cmd.Parameters.AddWithValue ("app", NpgsqlTypes.NpgsqlDbType.Varchar, applicationName);
using (var rdr = cmd.ExecuteReader ()) {
if (rdr.HasRows) {
while (rdr.Read ()) {
var profile = new PerformerProfile ();
profile.Id = rdr.GetInt64 (0);
profile.UserName = rdr.GetString (1);
profile.EMail = rdr.GetString (2);
profile.MEACode = MEACode;
profile.Rate = rdr.GetInt32 (3);
result.Add (profile);
}
}
}
}
}
return result.ToArray ();
}
/// <summary>
/// Gets the activity.
/// </summary>
/// <returns>The activity.</returns>
/// <param name="MEACode">MEA code.</param>
public Activity GetActivity (string MEACode)
{
Activity result = null;
using (NpgsqlConnection cnx = CreateConnection ()) {
cnx.Open ();
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText = @"select distinct meacode, title, cmnt, photo
from activity where meacode = :code and applicationname = :app";
cmd.Parameters.AddWithValue ("code", MEACode);
cmd.Parameters.AddWithValue ("app", applicationName);
using (var rdr = cmd.ExecuteReader ()) {
if (rdr.HasRows) {
rdr.Read ();
result = new Activity () {
Id = rdr.GetString (0),
Title = rdr.GetString (1),
Comment = rdr.GetString (2),
Photo = rdr.GetString (3)
};
}
}
}
cnx.Close ();
}
return result;
}
/// <summary>
/// Finds the activity.
/// </summary>
/// <returns>The activity.</returns>
/// <param name="pattern">Pattern.</param>
/// <param name="exerted">If set to <c>true</c> exerted.</param>
public Activity[] FindActivity (string pattern, bool exerted)
{
List<Activity> acties = new List<Activity> ();
using (NpgsqlConnection cnx = CreateConnection ()) {
cnx.Open ();
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText = (exerted) ?
@"SELECT a.meacode, a.title, a.cmnt, a.photo
FROM activity a, profiledata d, profiles p, users u
WHERE u.username = p.username
AND u.applicationname = p.applicationname
AND p.uniqueid = d.uniqueid
AND d.meacode = a.meacode
AND u.isapproved = TRUE
AND u.islockedout = FALSE
AND a.title like :pat
ORDER BY a.meacode " :
@"SELECT meacode, title, cmnt, photo
FROM activity
WHERE title LIKE :pat
ORDER BY meacode ";
cmd.Parameters.AddWithValue ("pat", pattern);
using (var rdr = cmd.ExecuteReader ()) {
if (!rdr.HasRows)
return new Activity[0];
new List<Activity> ();
while (rdr.Read ()) {
acties.Add (new Activity () {
Id = rdr.GetString (0),
Title = rdr.GetString (1),
Comment = rdr.GetString (2),
Photo = rdr.GetString (3)
});
}
}
}
cnx.Close ();
}
return acties.ToArray();
}
/// <summary>
/// Registers the activity.
/// </summary>
/// <param name="activity">Activity.</param>
/// <param name="code">Code.</param>
public void RegisterActivity (string activity, string code, string comment)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
cnx.Open ();
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText = "insert into activity (meacode,title,applicationname,cmnt) " +
" values (:code,:title,:app,:cmnt)";
cmd.Parameters.AddWithValue ("code", code);
cmd.Parameters.AddWithValue ("title", activity);
cmd.Parameters.AddWithValue ("app", applicationName);
cmd.Parameters.AddWithValue ("cmnt", comment);
cmd.ExecuteNonQuery ();
}
cnx.Close ();
}
}
/// <summary>
/// Registers the command.
/// </summary>
/// <returns>The command id in db.</returns>
/// <param name="com">COM.</param>
public long RegisterCommand (Command com)
{
long id;
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"insert into commandes (prdref,creation,params,clientname,applicationname) values (@pref,@creat,@prms,@cli,@app) returning id";
cmd.Parameters.AddWithValue ("@pref", com.ProductRef);
cmd.Parameters.AddWithValue ("@creat", com.CreationDate);
cmd.Parameters.AddWithValue ("@prms", JsonConvert.SerializeObject (com.Parameters));
cmd.Parameters.AddWithValue ("@cli", Membership.GetUser ().UserName);
cmd.Parameters.AddWithValue ("@app", ApplicationName);
cnx.Open ();
com.Id = id = (long)cmd.ExecuteScalar ();
}
}
return id;
}
/// <summary>
/// Gets the commands.
/// </summary>
/// <returns>The commands.</returns>
/// <param name="username">Username.</param>
public CommandSet GetCommands (string username)
{
// Check the user's authorisations
MembershipUser user = Membership.GetUser ();
if (user.UserName != username)
if (!Roles.IsUserInRole ("Admin"))
if (!Roles.IsUserInRole ("FrontOffice"))
throw new Exception ("Not allowed");
CommandSet cmds = new CommandSet ();
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select id,creation,prdref,params,class from commandes where @user = clientname and applicationname = @app";
cmd.Parameters.AddWithValue ("@user", username);
cmd.Parameters.AddWithValue ("@app", this.ApplicationName);
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Read ()) {
string cls = rdr.GetString (4);
Command ycmd = Command.CreateCommand(cls);
ycmd.Id = rdr.GetInt64 (0);
ycmd.CreationDate = rdr.GetDateTime (1);
ycmd.ProductRef = rdr.GetString (2);
object prms = JsonConvert.DeserializeObject<Dictionary<string,string>> (rdr.GetString (3));
ycmd.Parameters = prms as Dictionary<string,string>;
cmds.Add (ycmd);
}
}
}
cnx.Close ();
}
return cmds;
}
/// <summary>
/// Gets the stock status.
/// </summary>
/// <returns>The stock status.</returns>
/// <param name="productReference">Product reference.</param>
public virtual StockStatus GetStockStatus (string productReference)
{
return StockStatus.NonExistent;
}
/// <summary>
/// Gets the writting status changes.
/// </summary>
/// <returns>The writting statuses.</returns>
/// <param name="wrid">Wrid.</param>
public StatusChange[] GetWrittingStatuses (long wrid)
{
throw new NotImplementedException ();
}
/// <summary>
/// Gets the estimate status changes.
/// </summary>
/// <returns>The estimate statuses.</returns>
/// <param name="estid">Estid.</param>
public StatusChange[] GetEstimateStatuses (long estid)
{
throw new NotImplementedException ();
}
/// <summary>
/// Tags the writting.
/// </summary>
/// <param name="wrid">Wrid.</param>
/// <param name="tag">Tag.</param>
public void TagWritting (long wrid, string tag)
{
throw new NotImplementedException ();
}
/// <summary>
/// Drops the writting tag .
/// </summary>
/// <param name="wrid">Wrid.</param>
/// <param name="tag">Tag.</param>
public void DropWrittingTag (long wrid, string tag)
{
throw new NotImplementedException ();
}
/// <summary>
/// Sets the writting status.
/// </summary>
/// <param name="wrtid">Wrtid.</param>
/// <param name="status">Status.</param>
/// <param name="username">Username.</param>
public void SetWrittingStatus (long wrtid, int status, string username)
{
throw new NotImplementedException ();
}
/// <summary>
/// Sets the estimate status.
/// </summary>
/// <param name="estid">Estid.</param>
/// <param name="status">Status.</param>
/// <param name="username">Username.</param>
public void SetEstimateStatus (long estid, int status, string username)
{
throw new NotImplementedException ();
}
/// <summary>
/// Releases all resource used by the <see cref="Yavsc.NpgsqlContentProvider"/> object.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="Yavsc.NpgsqlContentProvider"/>. The
/// <see cref="Dispose"/> method leaves the <see cref="Yavsc.NpgsqlContentProvider"/> in an unusable state. After
/// calling <see cref="Dispose"/>, you must release all references to the <see cref="Yavsc.NpgsqlContentProvider"/> so
/// the garbage collector can reclaim the memory that the <see cref="Yavsc.NpgsqlContentProvider"/> was occupying.</remarks>
public void Dispose ()
{
throw new NotImplementedException ();
}
/// <summary>
/// Install the model in database using the specified cnx.
/// </summary>
/// <param name="cnx">Cnx.</param>
public void Install (System.Data.IDbConnection cnx)
{
throw new NotImplementedException ();
}
/// <summary>
/// Uninstall the module data and data model from
/// database, using the specified connection.
/// </summary>
/// <param name="cnx">Cnx.</param>
/// <param name="removeConfig">If set to <c>true</c> remove config.</param>
public void Uninstall (System.Data.IDbConnection cnx, bool removeConfig)
{
throw new NotImplementedException ();
}
/// <summary>
/// Defaults the config.
/// </summary>
/// <returns>The config.</returns>
/// <param name="appName">App name.</param>
/// <param name="cnxStr">Cnx string.</param>
public ConfigurationSection DefaultConfig (string appName, string cnxStr)
{
throw new NotImplementedException ();
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Yavsc.NpgsqlContentProvider"/> is active.
/// </summary>
/// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
public bool Active {
get {
throw new NotImplementedException ();
}
set {
throw new NotImplementedException ();
}
}
/// <summary>
/// Gets the different status labels.
/// 0 is the starting status. Each status is an integer and the 0-based index
/// of a string in this array.
/// </summary>
/// <value>The status labels.</value>
public string[] Statuses {
get {
return new string[] { "Created", "Validated", "Success", "Error" };
}
}
/// <summary>
/// Gets the final statuses.
/// </summary>
/// <value>The final statuses.</value>
public bool[] FinalStatuses {
get {
return new bool[] { false, false, true, true };
}
}
/// <summary>
/// Gets the estimates created by
/// or for the given user by user name.
/// </summary>
/// <returns>The estimates.</returns>
/// <param name="username">user name.</param>
public Estimate[] GetEstimates (string username)
{
if (username == null)
throw new InvalidOperationException (
"username cannot be" +
" null at searching for estimates");
List<long> ids = new List<long> ();
List<Estimate> ests = new List<Estimate> ();
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select _id from estimate where client = @uname or username = @uname";
cmd.Parameters.AddWithValue ("@uname", username);
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Read ()) {
ids.Add (rdr.GetInt64 (0));
}
rdr.Close ();
}
cnx.Close ();
}
foreach (long id in ids)
ests.Add (Get (id));
return ests.ToArray ();
}
}
/// <summary>
/// Gets the estimates.
/// </summary>
/// <returns>The estimates.</returns>
/// <param name="client">Client.</param>
/// <param name="responsible">Responsible.</param>
public Estimate[] GetEstimates (string client, string responsible)
{
if (client == null && responsible == null)
throw new InvalidOperationException (
"client and responsible cannot be" +
" both null at searching for estimates");
List<long> ids = new List<long> ();
List<Estimate> ests = new List<Estimate> ();
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select _id from estimate where ";
if (client != null) {
cmd.CommandText += "client = @clid";
if (responsible != null)
cmd.CommandText += " and ";
cmd.Parameters.AddWithValue ("@clid", client);
}
if (responsible != null) {
cmd.CommandText += "username = @resp";
cmd.Parameters.AddWithValue ("@resp", responsible);
}
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Read ()) {
ids.Add (rdr.GetInt64 (0));
}
rdr.Close ();
}
foreach (long id in ids)
ests.Add (Get (id));
return ests.ToArray ();
}
}
}
/// <summary>
/// Drops the writting.
/// </summary>
/// <param name="wrid">Wrid.</param>
public void DropWritting (long wrid)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"delete from writtings where _id = @wrid";
cmd.Parameters.AddWithValue ("@wrid", wrid);
cnx.Open ();
cmd.ExecuteNonQuery ();
}
}
}
/// <summary>
/// Drops the estimate.
/// </summary>
/// <param name="estid">Estid.</param>
public void DropEstimate (long estid)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"delete from estimate where _id = @estid";
cmd.Parameters.AddWithValue ("@estid", estid);
cnx.Open ();
cmd.ExecuteNonQuery ();
}
}
}
/// <summary>
/// Gets the estimate by identifier.
/// </summary>
/// <returns>The estimate.</returns>
/// <param name="estimid">Estimid.</param>
public Estimate Get (long estimid)
{
Estimate est = null;
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select title,username,client,description from estimate where _id = @estid";
cmd.Parameters.AddWithValue ("@estid", estimid);
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
if (!rdr.Read ()) {
return null;
}
est = new Estimate ();
est.Title = rdr.GetString (
rdr.GetOrdinal ("title"));
est.Responsible = rdr.GetString (
rdr.GetOrdinal ("username"));
int clientidx = rdr.GetOrdinal ("client");
if (!rdr.IsDBNull (clientidx))
est.Client = rdr.GetString (clientidx);
int index = rdr.GetOrdinal ("description");
if (!rdr.IsDBNull (index))
est.Description = rdr.GetString (index);
est.Id = estimid;
rdr.Close ();
}
}
// assert est != null
using (NpgsqlCommand cmdw = new NpgsqlCommand ("select _id, productid, ucost, count, description from writtings where estimid = @estid", cnx)) {
cmdw.Parameters.AddWithValue ("@estid", estimid);
using (NpgsqlDataReader rdrw = cmdw.ExecuteReader ()) {
List<Writting> lw = null;
if (rdrw.HasRows) {
lw = new List<Writting> ();
while (rdrw.Read ()) {
Writting w = new Writting ();
int dei = rdrw.GetOrdinal ("description");
if (!rdrw.IsDBNull (dei))
w.Description = rdrw.GetString (dei);
int opi = rdrw.GetOrdinal ("productid");
if (!rdrw.IsDBNull (opi))
w.ProductReference = rdrw.GetString (opi);
int oco = rdrw.GetOrdinal ("count");
if (!rdrw.IsDBNull (oco))
w.Count = rdrw.GetInt32 (oco);
int ouc = rdrw.GetOrdinal ("ucost");
if (!rdrw.IsDBNull (ouc))
w.UnitaryCost = rdrw.GetDecimal (ouc);
w.Id = rdrw.GetInt64 (rdrw.GetOrdinal ("_id"));
lw.Add (w);
}
est.Lines = lw.ToArray ();
}
}
}
return est;
}
}
/// <summary>
/// Updates the writting.
/// </summary>
/// <param name="wr">Wr.</param>
public void UpdateWritting (Writting wr)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"update writtings set " +
"description = @desc, " +
"ucost = @ucost, " +
"count = @count, " +
"productid = @prdid " +
"where _id = @wrid";
cmd.Parameters.AddWithValue ("@wrid", wr.Id);
cmd.Parameters.AddWithValue ("@desc", wr.Description);
cmd.Parameters.AddWithValue ("@ucost", wr.UnitaryCost);
cmd.Parameters.AddWithValue ("@prdid", wr.ProductReference);
cmd.Parameters.AddWithValue ("@count", wr.Count);
cnx.Open ();
cmd.ExecuteNonQuery ();
cnx.Close ();
}
}
}
/// <summary>
/// Saves the given Estimate object in database.
/// </summary>
/// <param name="estim">the Estimate object.</param>
public void Update (Estimate estim)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"update estimate set title = @tit, username = @un, " +
"description = @descr, client = @cli where _id = @estid";
cmd.Parameters.AddWithValue ("@tit", estim.Title);
cmd.Parameters.AddWithValue ("@un", estim.Responsible);
cmd.Parameters.AddWithValue ("@descr", estim.Description);
cmd.Parameters.AddWithValue ("@cli", estim.Client);
cmd.Parameters.AddWithValue ("@estid", estim.Id);
cnx.Open ();
cmd.ExecuteNonQuery ();
cnx.Close ();
}
}
}
/// <summary>
/// Add a line to the specified estimate by id,
/// using the specified desc, ucost, count and productid.
/// </summary>
/// <param name="estid">Estimate identifier.</param>
/// <param name="desc">Textual description for this line.</param>
/// <param name="ucost">Unitary cost.</param>
/// <param name="count">Cost multiplier.</param>
/// <param name="productid">Product identifier.</param>
public long Write (long estid, string desc, decimal ucost, int count, string productid)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"insert into writtings (description, estimid, ucost, count, productid) VALUES (@dscr,@estid,@ucost,@count,@prdid) returning _id";
cmd.Parameters.AddWithValue ("@dscr", desc);
cmd.Parameters.AddWithValue ("@estid", estid);
cmd.Parameters.AddWithValue ("@ucost", ucost);
cmd.Parameters.AddWithValue ("@count", count);
cmd.Parameters.AddWithValue ("@prdid", productid);
cnx.Open ();
long res = (long)cmd.ExecuteScalar ();
cnx.Close ();
return res;
}
}
}
/// <summary>
/// Sets the desc.
/// </summary>
/// <param name="writid">Writid.</param>
/// <param name="newDesc">New desc.</param>
public void SetDesc (long writid, string newDesc)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"update writtings set description = @dscr where _id = @writid";
cmd.Parameters.AddWithValue ("@tit", newDesc);
cmd.Parameters.AddWithValue ("@writid", writid);
cnx.Open ();
cmd.ExecuteNonQuery ();
cnx.Close ();
}
}
}
/// <summary>
/// Creates the estimate.
/// </summary>
/// <returns>The estimate.</returns>
/// <param name="client">Client.</param>
/// <param name="title">Title.</param>
/// <param name="responsible">Responsible.</param>
/// <param name="description">Description.</param>
public Estimate CreateEstimate (string responsible, string client, string title, string description)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"insert into estimate (title,description,username,client,applicationname) " +
"values (@tit,@descr,@resp,@un,@app) returning _id";
cmd.Parameters.AddWithValue ("@tit", title);
cmd.Parameters.AddWithValue ("@un", client);
cmd.Parameters.AddWithValue ("@resp", responsible);
cmd.Parameters.AddWithValue ("@descr", description);
cmd.Parameters.AddWithValue ("@app", ApplicationName);
cnx.Open ();
Estimate created = new Estimate ();
created.Id = (long)cmd.ExecuteScalar ();
cnx.Close ();
created.Title = title;
created.Description = description;
created.Client = client;
created.Responsible = responsible;
return created;
}
}
}
string applicationName = null;
/// <summary>
/// Gets or sets the name of the application.
/// </summary>
/// <value>The name of the application.</value>
public string ApplicationName {
get {
return applicationName;
}
set {
applicationName = value;
}
}
string cnxstr = null;
/// <summary>
/// Initialize this object using the specified name and config.
/// </summary>
/// <param name="name">Name.</param>
/// <param name="config">Config.</param>
public override void Initialize (string name, NameValueCollection config)
{
if (string.IsNullOrWhiteSpace (config ["connectionStringName"]))
throw new ConfigurationErrorsException ("No name for Npgsql connection string found");
cnxstr = ConfigurationManager.ConnectionStrings [config ["connectionStringName"]].ConnectionString;
applicationName = config ["applicationName"] ?? "/";
}
/// <summary>
/// Creates the connection.
/// </summary>
/// <returns>The connection.</returns>
protected NpgsqlConnection CreateConnection ()
{
return new NpgsqlConnection (cnxstr);
}
}
}