Example #1
0
 public static void Map(int listId, TodoItem entity, TodoListItemDto dto)
 {
     dto.Id          = entity.Key;
     dto.ItemId      = entity.ItemId;
     dto.Text        = entity.Text;
     dto.IsCompleted = entity.IsCompleted;
     dto.TodoList_Id = listId;
 }
Example #2
0
 public static TodoListItem ToDatabaseObject(this TodoListItemDto dto)
 {
     return(new TodoListItem()
     {
         Done = dto.Done,
         Color = dto.Color,
         Name = dto.Name,
         Id = dto.Id
     });
 }
Example #3
0
        public void Save(TodoListAggregate listEntity)
        {
            var listDto        = new TodoListDto();
            var resultItemList = new List <TodoListItemDto>();

            DtoMapper.Map(listEntity, listDto);
            using (var c = _sqlConnectionProvider.GetConnection())
            {
                using (var tran = c.BeginTransaction())
                {
                    try
                    {
                        if (listEntity.Key == 0)
                        {
                            const string insertTodoListSql = "INSERT INTO [TodoList]([ListId],[Name]) OUTPUT INSERTED.[Id] VALUES(@listId, @name)";
                            listDto.Id = c.QuerySingle <int>(insertTodoListSql, new { listId = listDto.ListId, name = listDto.Name }, tran);
                        }
                        else
                        {
                            //const string insertTodoListSql = "UPDATE [TodoList]([Name]) VALUES(@Name)";
                            c.Update(listDto, tran);
                        }

                        //remove deleted items
                        const string deleteRemovedSql = "DELETE FROM [TodoItem] WHERE [TodoList_Id] = @listId AND Id NOT IN @ids";
                        c.Execute(deleteRemovedSql, new { listId = listDto.Id, ids = listEntity.Items.Select(e => e.Key) }, tran);

                        foreach (var itemEntity in listEntity.Items)
                        {
                            var dto = new TodoListItemDto();
                            DtoMapper.Map(listDto.Id, itemEntity, dto);

                            if (itemEntity.Key == 0)
                            {
                                dto.Id = (int)c.Insert(dto, tran);
                            }
                            else if (itemEntity.Key > 0)
                            {
                                c.Update(dto, tran);
                            }

                            resultItemList.Add(dto);
                        }

                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
            }
        }
Example #4
0
            public static TodoItem From(TodoListItemDto dto)
            {
                var model = new TodoItem()
                {
                    Key         = dto.Id,
                    ItemId      = dto.ItemId,
                    IsCompleted = dto.IsCompleted,
                    Text        = dto.Text,
                };

                return(model);
            }
Example #5
0
        public async Task <TodoListItemDto> AddOrReplaceItem(Guid listId, TodoListItemDto item)
        {
            var listToEdit = Context.TodoLists.FirstOrDefault(x => x.Id == listId);

            var itemToReplace = listToEdit.Items.FirstOrDefault(x => x.Id == item.Id);

            var dbItem = item.ToDatabaseObject();

            if (itemToReplace == null)
            {
                listToEdit.Items.Add(dbItem);
            }
            else
            {
                listToEdit.Items.Remove(itemToReplace);
                listToEdit.Items.Add(dbItem);
            }

            await Context.SaveChangesAsync();

            return(dbItem.ToDto());
        }