Example #1
0
        public async Task RemovePetSitter(AddOrRemovePetSitterDto dto)
        {
            if (!dto.PetId.HasValue)
            {
                var userId = await AuthManager.GetUserIdAsync(dto.UserName);

                if (string.IsNullOrEmpty(userId))
                {
                    throw new Exception(ExceptionMessages.NotFound);
                }

                var existingSittings = await DbContext.PetSittings.Where(ps => ps.Pet.OwnerId == KutyAppContext.CurrentUser.Id && ps.SitterId.Equals(userId)).ToListAsync();

                if (existingSittings.Any())
                {
                    DbContext.PetSittings.RemoveRange(existingSittings);
                }
            }
            else
            {
                var userId = await AuthManager.GetUserIdAsync(dto.UserName);

                var petSitting = await DbContext.PetSittings.Include(ps => ps.Pet).FirstOrDefaultAsync(ps => ps.PetId == dto.PetId && ps.SitterId == userId);

                if (petSitting != null && petSitting.Pet.OwnerId == KutyAppContext.CurrentUser.Id)
                {
                    petSitting.Pet = null;
                    DbContext.PetSittings.Remove(petSitting);
                }
            }

            await DbContext.SaveChangesAsync();
        }
Example #2
0
        //TODO: ha nincs petid akkor a user osszes allatahoz
        public async Task AddPetSitter(AddOrRemovePetSitterDto dto)
        {
            if (!dto.PetId.HasValue)
            {
                var userId = await AuthManager.GetUserIdAsync(dto.UserName);

                if (string.IsNullOrEmpty(userId))
                {
                    throw new Exception(ExceptionMessages.NotFound);
                }

                var existingSitterIds = await DbContext.PetSittings.Where(ps => ps.Pet.OwnerId == KutyAppContext.CurrentUser.Id).Select(ps => ps.SitterId).Distinct().ToListAsync();

                if (!existingSitterIds.Contains(userId))
                {
                    var myPetIds = await DbContext.Pets.Where(p => p.OwnerId == KutyAppContext.CurrentUser.Id).Select(p => p.Id).ToListAsync();

                    myPetIds.ForEach(id => DbContext.PetSittings.Add(new PetSitting {
                        PetId = id, SitterId = userId
                    }));
                }
            }
            else
            {
                bool petExists = await DbContext.Pets.AnyAsync(p => p.Id == dto.PetId);

                var userId = await AuthManager.GetUserIdAsync(dto.UserName);

                if (petExists && !string.IsNullOrWhiteSpace(userId))
                {
                    DbContext.PetSittings.Add(new PetSitting {
                        PetId = dto.PetId.Value, SitterId = userId
                    });
                }
                else
                {
                    throw new Exception(ExceptionMessages.NotFound);
                }
            }

            await DbContext.SaveChangesAsync();
        }
Example #3
0
 public async Task <ActionResult> RemovePetSitter(AddOrRemovePetSitterDto dto) =>
 await ResultAsync(PetManager.RemovePetSitter(dto));