Advertisement
NeonDriver

Service

Sep 25th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System.Linq;
  2.  
  3. namespace Terrasoft.Configuration
  4. {
  5.  
  6.     using System.ServiceModel;
  7.     using System.ServiceModel.Web;
  8.     using System.ServiceModel.Activation;
  9.     using System.Web;
  10.     using System;
  11.     using System.Collections.Generic;
  12.     using System.Collections.ObjectModel;
  13.     using System.Data;
  14.     using Terrasoft.Common;
  15.     using Terrasoft.Core.DB;
  16.     using Terrasoft.Core.Entities;
  17.     using Terrasoft.Core;
  18.  
  19.     [ServiceContract]
  20.     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  21.     public class StorageService
  22.     {
  23.         private UserConnection _userConnection;
  24.         private UserConnection UserConnection {
  25.             get {
  26.                 return _userConnection ?? (_userConnection = HttpContext.Current
  27.                 .Session["UserConnection"] as UserConnection);
  28.             }
  29.         }
  30.        
  31.         #region Constructors: Public
  32.  
  33.         public StorageService() {}
  34.  
  35.         public StorageService(UserConnection userConnection) {
  36.             _userConnection = userConnection;
  37.         }
  38.  
  39.         #endregion
  40.        
  41.         private decimal GetEntityArea(Guid entityId, string tableName)
  42.         {
  43.             // FIRST VARIANT
  44.             //EntitySchema schema = UserConnection.EntitySchemaManager.FindInstanceByName(tableName);
  45.             //Entity entity = schema.CreateEntity(UserConnection);
  46.             //entity.FetchFromDB("Id", entityId, new[] {"UsrArea"});
  47.  
  48.             // SECOND VARIANT
  49.             var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, tableName);
  50.             esqResult.AddColumn("UsrArea");
  51.  
  52.             var storageCapacityEntity = esqResult.GetEntity(UserConnection, entityId);
  53.  
  54.             return storageCapacityEntity.GetTypedColumnValue<decimal>("UsrArea");
  55.         }
  56.        
  57.         private decimal GetUsedStorageArea(Guid storageId) {
  58.             // FIRST VARIANT
  59.             //var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "UsrStorage");
  60.             //var totalUsrAreaColumn = esqResult.AddColumn(esqResult.CreateAggregationFunction(AggregationTypeStrict.Sum, "[UsrStuff:UsrStorage:Id].UsrArea"));
  61.             //esqResult.Filters.Add(esqResult.CreateFilterWithParameters(FilterComparisonType.Equal, "Id", storageId));
  62.             //var storageUsedCapacityEntity = esqResult.GetEntityCollection(UserConnection).FirstOrDefault();
  63.  
  64.             //return storageUsedCapacityEntity.GetTypedColumnValue<decimal>(totalUsrAreaColumn.Name);
  65.  
  66.             // SECOND VARIANT
  67.             var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "UsrStuff");
  68.             esqResult.AddColumn("UsrArea");
  69.             esqResult.Filters.Add(esqResult.CreateFilterWithParameters(FilterComparisonType.Equal, "UsrStorage", storageId));
  70.             var entityCollection = esqResult.GetEntityCollection(UserConnection);
  71.             var resultUsrArea = entityCollection.Sum(x => x.GetTypedColumnValue<decimal>("UsrArea"));
  72.             return resultUsrArea;
  73.         }
  74.        
  75.         [OperationContract]
  76.         [WebInvoke(Method = "GET", UriTemplate = "CheckStorageCapacity?storageId={storageId}&stuffId={stuffId}", ResponseFormat = WebMessageFormat.Json)]
  77.         public bool CheckStorageCapacity(Guid storageId, Guid stuffId)
  78.         {
  79.             var stuffArea = GetEntityArea(stuffId, "UsrStuff");
  80.             var storageArea = GetEntityArea(storageId, "UsrStorage");
  81.             var storageUsedArea = GetUsedStorageArea(storageId);
  82.             var freeStorageArea = storageArea - storageUsedArea;
  83.  
  84.             return stuffArea <= freeStorageArea;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement