示例#1
0
        public async Task <ServiceResult <CatFeedingModel> > FeedAsync(CatFeedingCreateModel info)
        {
            CatInDbModel cat = await catDatabase.GetAsync(info.CatId);

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

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

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

            if (!await catSharingDatabase.IsCatSharedWithUserAsync(info.UserId, info.CatId))
            {
                return(new ServiceResult <CatFeedingModel>(ServiceResultStatus.ActionNotAllowed, "You cannot feed this cat"));
            }

            await catFeedingdatabase.CreateAsync(mapper.Map <CatFeedingCreateInDbModel, CatFeedingCreateModel>(info));

            return(new ServiceResult <CatFeedingModel>(ServiceResultStatus.NoContent));
        }
示例#2
0
        public async Task <ServiceResult <List <DateTime> > > GetFeedingForPeriodAsync(int userId, int catId, DateTime start, DateTime finish)
        {
            UserInDbModel user = await userDatabase.GetAsync(userId);

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

            CatInDbModel cat = await catDatabase.GetAsync(catId);

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

            List <DateTime>            info     = new List <DateTime>();
            List <CatFeedingInDbModel> feedings = await catFeedingdatabase.GetFeedingsForPeriodAsync(userId, catId, start, finish);

            for (int i = 0; i < feedings.Count; i++)
            {
                info.Add(feedings[i].FeedingTime);
            }
            return(new ServiceResult <List <DateTime> >(ServiceResultStatus.ItemRecieved, info));
        }
示例#3
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));
        }
示例#4
0
        public async Task <ServiceResult <CatGetServiceModel> > GetAsync(int id)
        {
            CatInDbModel cat = await catDatabase.GetAsync(id);

            if (cat == null)
            {
                return(new ServiceResult <CatGetServiceModel>(ServiceResultStatus.ItemNotFound, "Cat is not found"));
            }
            return(new ServiceResult <CatGetServiceModel>(ServiceResultStatus.ItemRecieved, mapper.Map <CatGetServiceModel, CatInDbModel>(cat)));
        }
 private void SetUpMockRepositories(
     int catSharingCreateResult,
     bool isCatShared,
     CatInDbModel catGetResult,
     UserInDbModel userGetResult,
     CatSharingCreateInDbModel mapperResult)
 {
     mockCatSharingDatabase.Setup(repository => repository.CreateAsync(catSharingCreateInDb)).Returns(Task.FromResult(catSharingCreateResult));
     mockCatSharingDatabase.Setup(repository => repository.IsCatSharedWithUserAsync(1, 1)).Returns(Task.FromResult(isCatShared));
     mockCatDatabase.Setup(repository => repository.GetAsync(1)).Returns(Task.FromResult(catGetResult));
     mockUserDatabase.Setup(repository => repository.GetAsync(1)).Returns(Task.FromResult(userGetResult));
     mockMapper.Setup(mapper => mapper.Map <CatSharingCreateInDbModel, CatSharingCreateModel>(catSharingCreate)).Returns(mapperResult);
 }