Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- namespace Terrasoft.Configuration
- {
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.ServiceModel.Activation;
- using System.Web;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Data;
- using Terrasoft.Common;
- using Terrasoft.Core.DB;
- using Terrasoft.Core.Entities;
- using Terrasoft.Core;
- [ServiceContract]
- [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
- public class StorageService
- {
- private UserConnection _userConnection;
- private UserConnection UserConnection {
- get {
- return _userConnection ?? (_userConnection = HttpContext.Current
- .Session["UserConnection"] as UserConnection);
- }
- }
- #region Constructors: Public
- public StorageService() {}
- public StorageService(UserConnection userConnection) {
- _userConnection = userConnection;
- }
- #endregion
- private decimal GetEntityArea(Guid entityId, string tableName)
- {
- // FIRST VARIANT
- //EntitySchema schema = UserConnection.EntitySchemaManager.FindInstanceByName(tableName);
- //Entity entity = schema.CreateEntity(UserConnection);
- //entity.FetchFromDB("Id", entityId, new[] {"UsrArea"});
- // SECOND VARIANT
- var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, tableName);
- esqResult.AddColumn("UsrArea");
- var storageCapacityEntity = esqResult.GetEntity(UserConnection, entityId);
- return storageCapacityEntity.GetTypedColumnValue<decimal>("UsrArea");
- }
- private decimal GetUsedStorageArea(Guid storageId) {
- // FIRST VARIANT
- //var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "UsrStorage");
- //var totalUsrAreaColumn = esqResult.AddColumn(esqResult.CreateAggregationFunction(AggregationTypeStrict.Sum, "[UsrStuff:UsrStorage:Id].UsrArea"));
- //esqResult.Filters.Add(esqResult.CreateFilterWithParameters(FilterComparisonType.Equal, "Id", storageId));
- //var storageUsedCapacityEntity = esqResult.GetEntityCollection(UserConnection).FirstOrDefault();
- //return storageUsedCapacityEntity.GetTypedColumnValue<decimal>(totalUsrAreaColumn.Name);
- // SECOND VARIANT
- var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "UsrStuff");
- esqResult.AddColumn("UsrArea");
- esqResult.Filters.Add(esqResult.CreateFilterWithParameters(FilterComparisonType.Equal, "UsrStorage", storageId));
- var entityCollection = esqResult.GetEntityCollection(UserConnection);
- var resultUsrArea = entityCollection.Sum(x => x.GetTypedColumnValue<decimal>("UsrArea"));
- return resultUsrArea;
- }
- [OperationContract]
- [WebInvoke(Method = "GET", UriTemplate = "CheckStorageCapacity?storageId={storageId}&stuffId={stuffId}", ResponseFormat = WebMessageFormat.Json)]
- public bool CheckStorageCapacity(Guid storageId, Guid stuffId)
- {
- var stuffArea = GetEntityArea(stuffId, "UsrStuff");
- var storageArea = GetEntityArea(storageId, "UsrStorage");
- var storageUsedArea = GetUsedStorageArea(storageId);
- var freeStorageArea = storageArea - storageUsedArea;
- return stuffArea <= freeStorageArea;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement