示例#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));
        }