示例#1
0
        /// <summary> Constructor. </summary>
        /// <param name="fileRecords"> The file records. </param>
        /// <param name="savingsType"> (Optional) Type of the size savings to calculate. </param>
        public DuplicateFileGroup(IEnumerable <FileRecord> fileRecords, SavingsType savingsType = SavingsType.SaveBiggest)
        {
            this.FileRecords = new List <FileRecord>(fileRecords);

            //Precalculate stats.
            this.Hash  = this.FileRecords.Select(fr => fr.Hash).FirstOrDefault();
            this.Count = this.FileRecords.Count();

            this.TotalSize = this.FileRecords.Sum(fr => fr.Size);

            switch (savingsType)
            {
            case SavingsType.SaveBiggest:
                this.PotentialSizeSaving = this.FileRecords.OrderByDescending(fr => fr.Size).Skip(1).Sum(fr => fr.Size);
                break;

            case SavingsType.SaveSmallest:
                this.PotentialSizeSaving = this.FileRecords.OrderBy(fr => fr.Size).Skip(1).Sum(fr => fr.Size);
                break;

            case SavingsType.SaveMedian:
                //This is kind of hacky, but good enough for our purposes here. CLOSE ENOUGH
                var medianFileRecord = this.FileRecords.OrderBy(fr => fr.Size).ElementAt(this.Count / 2);
                this.PotentialSizeSaving = this.FileRecords.Except(new[] { medianFileRecord }).Sum(fr => fr.Size);
                break;

            default:
                break;
            }
        }
        public async Task <IHttpActionResult> PutSavingsType(int id, SavingsType savingsType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != savingsType.ID)
            {
                return(BadRequest());
            }

            db.Entry(savingsType).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SavingsTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> GetSavingsType(int id)
        {
            SavingsType savingsType = await db.SavingsTypes.FindAsync(id);

            if (savingsType == null)
            {
                return(NotFound());
            }

            return(Ok(savingsType));
        }
        public async Task <IHttpActionResult> PostSavingsType(SavingsType savingsType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.SavingsTypes.Add(savingsType);
            await db.SaveChangesAsync();

            //return CreatedAtRoute("DefaultApi", new { id = savingsType.ID }, savingsType);
            return(Ok <SavingsType>(savingsType));
        }
示例#5
0
        public static SavingsStrategy GetStragery(SavingsType type)
        {
            switch (type)
            {
            case SavingsType.Fixed:
                return(new FixedSavingsStrategy());

            case SavingsType.Percentage:
                return(new PercentageSavingsStrategy());

            default:
                return(new FixedSavingsStrategy());
            }
        }
        public async Task <IHttpActionResult> DeleteSavingsType(int id)
        {
            SavingsType savingsType = await db.SavingsTypes.FindAsync(id);

            if (savingsType == null)
            {
                return(NotFound());
            }

            db.SavingsTypes.Remove(savingsType);
            await db.SaveChangesAsync();

            return(Ok(savingsType));
        }