public async Task <Shisha> MoveWishedToSmoked(Shisha shisha)
        {
            var update = Builders <Shisha> .Update.Set("IsWish", false).Set("Count", 1);

            await this.shishasRepository.UpdateOne(shisha.Id, update);

            var currentUser = await this.usersService.GetCurrentUser();

            var myWished = currentUser.ShishasWishedList.ToList();

            if (myWished.Contains(shisha.Id))
            {
                myWished.Remove(shisha.Id);

                await this.usersService.UpdateFields(currentUser.Id, new Dictionary <string, object>
                {
                    { "ShishasWishedList", myWished.ToArray() }
                });
            }

            var mySmoked = currentUser.ShishasSmokedList.ToList();

            if (!mySmoked.Contains(shisha.Id))
            {
                mySmoked.Add(shisha.Id);

                await this.usersService.UpdateFields(currentUser.Id, new Dictionary <string, object>
                {
                    { "ShishasSmokedList", mySmoked.ToArray() }
                });
            }

            return(shisha);
        }
        public async Task <Shisha> UpdateCount(Shisha shisha)
        {
            var update = Builders <Shisha> .Update.Set("Count", shisha.SmokedCount + 1);

            await this.shishasRepository.UpdateOne(shisha.Id, update);

            return(shisha);
        }
        public double GetShishaPower(Shisha shisha)
        {
            double power = 0;
            int    count = shisha.Tabaccos.Length;

            foreach (var tabacco in shisha.Tabaccos)
            {
                power += tabacco.Power;
            }
            return(power / count);
        }
        public async Task <Shisha> AddToSmokeList(Shisha shisha, string publicId, string newIdentifier)
        {
            if (newIdentifier != null)
            {
                shisha = await this.CreateShishaFromIdentifier(newIdentifier);
            }
            var currentUser = await this.usersService.GetCurrentUser();

            var shishaIdentifier = this.GetShishaIdentifier(shisha);

            var findSmokedShisha = await this.FindShishaByIdentifier(currentUser.ShishasSmokedList, shishaIdentifier);

            if (findSmokedShisha)
            {
                throw new ItemAlreadyExistException(shisha, MessageCodes.ALREADY_EXISTS_IN_SMOKED);
            }

            var newShisha = new Shisha()
            {
                Identifier = shishaIdentifier,
                PublicId   = publicId ?? null,
                UserId     = currentUser.Id,
                Name       = shisha.Name,
                //Description = shisha.Description,
                //Type = shisha.Type,
                //Tag = shisha.Tag,
                SmokedCount = 1,
                IsPublic    = false,
                IsWish      = false,
                IsFavourite = false,
                IsSeasonal  = false,
                Power       = this.GetShishaPower(shisha),
                Tabaccos    = shisha.Tabaccos,
                //  TotalRating
                //  OwnRating
            };

            await shishasRepository.AddOne(newShisha);

            var newSmoked = currentUser.ShishasSmokedList.ToList();

            newSmoked.Add(newShisha.Id);
            await this.usersService.UpdateFields(currentUser.Id, new Dictionary <string, object>
            {
                { "ShishasSmokedList", newSmoked.ToArray() }
            });

            return(newShisha);
        }
        //only for admin(global) shishas
        public async Task <Shisha> Add(Shisha shisha)
        {
            var currentUser = await this.usersService.GetCurrentUser();

            var shishaKey = this.GetShishaIdentifier(shisha);
            //TODO allow only for admin to continue

            var publicShishas = await this.GetPublicShishas();

            var findPublicShisha = publicShishas?.FirstOrDefault(s => s.Identifier == shishaKey);

            if (findPublicShisha != null)
            {
                throw new ItemAlreadyExistException(shisha);
            }

            var newShisha = new Shisha()
            {
                Identifier = shishaKey,
                PublicId   = null,
                UserId     = currentUser.Id,
                Name       = shisha.Name,
                //Description = shisha.Description,
                //Type = shisha.Type,
                //Tag = shisha.Tag,
                IsPublic    = true,
                IsWish      = false,
                IsFavourite = false,
                IsSeasonal  = false, //TODO maybe
                Power       = this.GetShishaPower(shisha),
                Tabaccos    = shisha.Tabaccos,
                TotalRating = 1,
                OwnRating   = 0,
            };

            await shishasRepository.AddOne(newShisha);

#pragma warning disable 4014
            this.activityService.Add(new Activity()
            {
                UserId = currentUser.Id,
                ItemId = newShisha.Id,
                Type   = ActivityTypeEnum.GlobalShishaCreated
            });
#pragma warning restore 4014

            return(newShisha);
        }
        public string GetShishaIdentifier(Shisha shisha)
        {
            List <int> tabaccos = new List <int>();

            foreach (var tabacco in shisha.Tabaccos)
            {
                tabaccos.Add(tabacco.InternalId);
            }

            if (tabaccos.GroupBy(n => n).Any(c => c.Count() > 1))
            {
                throw new DuplicateItemsKeyException(shisha);
            }

            tabaccos.Sort();
            return(string.Join(",", tabaccos));
        }
示例#7
0
 public static ShishaDTO ToDto(this Shisha shisha) => new ShishaDTO
 {
     Id             = shisha.Id,
     Identifier     = shisha.Identifier,
     Name           = shisha.Name,
     Description    = shisha.Description,
     Type           = shisha.Type,
     Tag            = shisha.Tag,
     Picture        = shisha.Picture,
     Power          = shisha.Power,
     IsPublic       = shisha.IsPublic,
     IsFavourite    = shisha.IsFavourite,
     IsWish         = shisha.IsWish,
     IsSeasonal     = shisha.IsSeasonal,
     CreatedAt      = ((long)(shisha.CreatedAt - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(),
     CreatedBy      = shisha.CreatedBy,
     LastModifiedAt = ((long)(shisha.CreatedAt - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(),
     LastModifiedBy = shisha.LastModifiedBy
 };
        public async Task <Shisha> AddToWishList(Shisha shisha)
        {
            var currentUser = await this.usersService.GetCurrentUser();

            var shishaIdentifier = this.GetShishaIdentifier(shisha);

            var findSmokedShisha = await this.FindShishaByIdentifier(currentUser.ShishasSmokedList, shishaIdentifier);

            if (findSmokedShisha)
            {
                throw new ItemAlreadyExistException(shisha, MessageCodes.ALREADY_EXISTS_IN_SMOKED);
            }

            var findWishedShisha = await this.FindShishaByIdentifier(currentUser.ShishasWishedList, shishaIdentifier);

            if (findWishedShisha)
            {
                throw new ItemAlreadyExistException(shisha, MessageCodes.ALREADY_EXISTS_IN_WISHED);
            }

            var newShisha = new Shisha()
            {
                Identifier = shishaIdentifier,
                PublicId   = null,
                UserId     = currentUser.Id,
                Name       = shisha.Name,
                //Description = shisha.Description,
                //Type = shisha.Type,
                //Tag = shisha.Tag,
                SmokedCount = 0,
                IsPublic    = false,
                IsWish      = true,
                IsFavourite = false,
                IsSeasonal  = false,
                Power       = this.GetShishaPower(shisha),
                Tabaccos    = shisha.Tabaccos,
                //  TotalRating
                //  OwnRating
            };

            await shishasRepository.AddOne(newShisha);

            var newWished = currentUser.ShishasWishedList.ToList();

            newWished.Add(newShisha.Id);
            await this.usersService.UpdateFields(currentUser.Id, new Dictionary <string, object>
            {
                { "ShishasWishedList", newWished.ToArray() }
            });

#pragma warning disable 4014
            this.activityService.Add(new Activity()
            {
                UserId = currentUser.Id,
                ItemId = newShisha.Id,
                Type   = ActivityTypeEnum.UserShishaCreated
            });
#pragma warning restore 4014

            return(newShisha);
        }