Exemplo n.º 1
0
 public async Task CreateManyRecords(IEnumerable <RecordsViewModel> records)
 {
     foreach (var item in records)
     {
         await _recordRepository.Create(_mapper.Map <RecordsViewModel, Record>(item));
     }
     await _recordRepository.Save();
 }
Exemplo n.º 2
0
        public static async Task <TId> TestCreate <T, TId, TQuery>(this IRecordRepository <T, TId, TQuery> repository, T value) where TId : class where T : RHasId <TId> where TQuery : QueryRequest, new()
        {
            var result = await repository.Create(value);

            Assert.IsNotNull(result);

            var item = await repository.Get(result);

            item.WithDeepEqual(value).IgnoreSourceProperty(x => x.Id);

            return(result);
        }
Exemplo n.º 3
0
        public async Task SyncRecordRepository <T, TId, TQuery>(IRecordRepository <T, TId, TQuery> source, IRecordRepository <T, TId, TQuery> target, bool full) where TId : class where T : RHasId <TId> where TQuery : QueryRequest, new()
        {
            HashSet <TId> remoteIds = await target.All().ToHashSetAsync();

            await foreach (var item in source.GetAllItems().IgnoreNull())
            {
                Logger.LogInformation($"Loaded {item.Id}");
                if (remoteIds.Contains(item.Id))
                {
                    var result = await target.Update(item);

                    if (result)
                    {
                        Logger.LogInformation($"Updated {item.Id}");
                    }
                    else
                    {
                        Logger.LogError($"Failed to update {item.Id}");
                    }
                }
                else
                {
                    var result = await target.Create(item);

                    if (result is null)
                    {
                        Logger.LogError($"Failed to create {item.Id}");
                    }
                    else
                    {
                        Logger.LogInformation($"Created {item.Id}");
                    }
                }
                remoteIds.Remove(item.Id);
            }
            if (full)
            {
                foreach (var v in remoteIds)
                {
                    var result = await target.Delete(v);

                    if (result)
                    {
                        Logger.LogInformation($"Deleted {v}.");
                    }
                    else
                    {
                        Logger.LogError($"Failed to deleted {v}.");
                    }
                }
            }
        }
        public async Task <RecordResponse> SaveAsync(Record record)
        {
            try
            {
                await _recordRepository.Create(record);

                await _unitOfWork.CompleteAsync();

                return(new RecordResponse(record));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new RecordResponse($"An error occurred when saving the record: {ex.GetAllMessages()}"));
            }
        }
Exemplo n.º 5
0
        public IActionResult NewRecord(Record record, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                if (image != null && image.Length < 16777216)
                {
                    record.ImageMimeType = image.ContentType;
                    using (BinaryReader reader = new BinaryReader(image.OpenReadStream()))
                    {
                        record.ImageData = reader.ReadBytes((int)image.Length);
                    }
                }
                else
                {
                    using (BinaryReader reader = new BinaryReader(new FileStream("wwwroot/img/placeholder.jpg", FileMode.Open)))
                    {
                        FileInfo info = new FileInfo("wwwroot/img/placeholder.jpg");
                        record.ImageData     = reader.ReadBytes((int)info.Length);
                        record.ImageMimeType = "image/jpeg";
                    }
                }

                if (record.RecordId == 0)
                {
                    recordRepository.Create(record);
                }
                else
                {
                    Record old = recordRepository.GetById(record.RecordId);
                    old.Title         = record.Title;
                    old.Type          = record.Type;
                    old.Description   = record.Description;
                    old.GenreId       = record.GenreId;
                    old.ArtistId      = record.ArtistId;
                    old.Amount        = record.Amount;
                    old.ImageData     = record.ImageData;
                    old.ImageMimeType = record.ImageMimeType;
                    recordRepository.Update(old);
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(record));
        }
Exemplo n.º 6
0
 public void Create(RecordModel entity)
 {
     _recordRepository.Create(RecordMapper.ToEntity(entity));
 }
Exemplo n.º 7
0
 public void Create(Record item)
 {
     repository.Create(item);
 }