fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.3k stars 1.59k forks source link

GetById Repositories return first object if no specification define #139

Closed ghaith100994 closed 3 years ago

ghaith100994 commented 3 years ago

public async Task GetByIdAsync<T, TDto> in Repositories Class return first item of table if speciation has not been initialize

iammukeshm commented 3 years ago

This was already fixed in the new release. Which version of the nuget package are you using?

ghaith100994 commented 3 years ago

rc-3

ghaith100994 commented 3 years ago
public async Task<TDto> GetByIdAsync<T, TDto>(Guid entityId, BaseSpecification<T> specification, CancellationToken cancellationToken = default)
        where T : BaseEntity
        where TDto : IDto
        {
            string cacheKey = CacheKeys.GetCacheKey<T>(entityId);
            byte[] cachedData = !string.IsNullOrWhiteSpace(cacheKey) ? await _cache.GetAsync(cacheKey, cancellationToken) : null;
            if (cachedData != null)
            {
                await _cache.RefreshAsync(cacheKey, cancellationToken);
                var entity = _serializer.Deserialize<TDto>(Encoding.Default.GetString(cachedData));
                return entity;
            }
            else
            {
                IQueryable<T> query = _dbContext.Set<T>();
                if (specification != null)
                    query = query.Specify(specification).Where(a => a.Id == entityId);
                var entity = await query.FirstOrDefaultAsync(cancellationToken: cancellationToken);
                var dto = entity.Adapt<TDto>();
                if (dto != null)
                {
                    if ((specification?.Includes?.Count == 0) || specification == null)
                    {
                        var options = new DistributedCacheEntryOptions();
                        byte[] serializedData = Encoding.Default.GetBytes(_serializer.Serialize(dto));
                        await _cache.SetAsync(cacheKey, serializedData, options, cancellationToken);
                    }

                    return dto;
                }

                throw new EntityNotFoundException(string.Format(_localizer["entity.notfound"], typeof(T).Name, entityId));
            }
        }

here when specification is null no filtering is done

iammukeshm commented 3 years ago

Please check RC 4 . This bug was fixed. Do let me know if you face any additional issues related to this. Thanks

https://www.nuget.org/packages/FullStackHero.WebAPI.Boilerplate/0.0.4-rc

ghaith100994 commented 3 years ago

the bug still exit in the RC 4 I think you have fixed it for this function public async Task<T> GetByIdAsync<T>(Guid entityId, BaseSpecification<T> specification = null, CancellationToken cancellationToken = default) not the other one public async Task<TDto> GetByIdAsync<T, TDto>(Guid entityId, BaseSpecification<T> specification, CancellationToken cancellationToken = default)

the answer if (specification != null) query = query.Specify(specification).Where(a => a.Id == entityId); else query = query.Where(a => a.Id == entityId);

iammukeshm commented 3 years ago

Thanks for pointing it out. Could you test it now? I have added the changes.

iammukeshm commented 3 years ago

Please pull the latest code from the repository and try.

ghaith100994 commented 3 years ago

Thanks It's Work