Beispiel #1
0
        public async Task <ServiceResult <CatServiceModel> > CreateAsync(CatCreateServiceModel info, IEnumerable <Claim> userClaims)
        {
            int   userId   = Convert.ToInt32(userClaims.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier).Value);
            Roles userRole = (Roles)Convert.ToInt32(userClaims.FirstOrDefault(claim => claim.Type == ClaimTypes.Role).Value);

            if (userId != info.OwnerId)
            {
                if (userRole != Roles.Admin)
                {
                    return(new ServiceResult <CatServiceModel>(ServiceResultStatus.ActionNotAllowed, "You cannot create cat for this user"));
                }
            }

            UserInDbModel user = await userDatabase.GetAsync(info.OwnerId);

            if (user == null)
            {
                return(new ServiceResult <CatServiceModel>(ServiceResultStatus.ItemNotFound, "User is not found"));
            }

            int catId = await catDatabase.CreateAsync(mapper.Map <CatCreateInDbModel, CatCreateServiceModel>(info));

            CatServiceModel cat = new CatServiceModel(catId, info.Name, info.OwnerId);
            await catSharingDatabase.CreateAsync(new CatSharingCreateInDbModel(cat.Id, info.OwnerId));

            return(new ServiceResult <CatServiceModel>(ServiceResultStatus.ItemCreated, cat));
        }
Beispiel #2
0
        public async Task <ServiceResult <CatSharingModel> > ShareAsync(CatSharingCreateModel info, int ownerId)
        {
            UserInDbModel userInDb = await userDatabase.GetAsync(info.UserId);

            if (userInDb == null)
            {
                return(new ServiceResult <CatSharingModel>(ServiceResultStatus.ItemNotFound, "User to share cannot be found"));
            }

            CatInDbModel cat = await catDatabase.GetAsync(info.CatId);

            if (cat == null)
            {
                return(new ServiceResult <CatSharingModel>(ServiceResultStatus.ItemNotFound, "Cat is not found"));
            }

            if (cat.OwnerId != ownerId)
            {
                return(new ServiceResult <CatSharingModel>(ServiceResultStatus.CantShareWithUser, "This user cannot share the cat"));
            }

            if (!await IsCatSharedWithUser(info.UserId, info.CatId))
            {
                await catSharingDatabase.CreateAsync(mapper.Map <CatSharingCreateInDbModel, CatSharingCreateModel>(info));
            }
            return(new ServiceResult <CatSharingModel>(ServiceResultStatus.CatIsShared));
        }