Exemplo n.º 1
0
        private async Task CreateTopicIfNotExist(int id)
        {
            var topic = await _ctx.Topics.Get(id);

            if (topic == null)
            {
                var article = await _ctx.Articles.Get((int)id);

                if (article != null)
                {
                    var newTopic = new DbTopic
                    {
                        Id             = article.Id,
                        AnnouncementId = article.Id,
                        AuthorId       = article.UserId,
                        Created        = DateTime.Now,
                        Updated        = DateTime.Now,
                        Title          = article.Title,
                        Vendor         = article.Vendor,
                        VendorCode     = article.Code
                    };
                    await _ctx.Topics.Create(newTopic);
                }
            }
        }
Exemplo n.º 2
0
        public async Task InsertTest()
        {
            var ctx  = new DbContext(_cs);
            var item = new DbTopic()
            {
                Id             = _id,
                Title          = "test",
                AnnouncementId = _id,
                AuthorId       = _creatorId,
                Vendor         = "TestVendor",
                VendorCode     = "TestVendorCode",
                Created        = new DateTime(2019, 1, 1, 22, 23, 00)
            };
            DbTopic ins;

            try
            {
                ins = await ctx.Topics.Create(item);

                Assert.AreEqual(item.Id, ins.Id);
            }
            catch (MySqlException ex)
            {
                Debug.WriteLine(ex.Message);
                await ctx.Topics.Delete(_id);

                ins = await ctx.Topics.Create(item);

                Assert.AreEqual(item.Id, ins.Id);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Создание топика
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public async Task <DbTopic> Create(DbTopic item)
        {
            var  sqlQuery = @"INSERT INTO dbtopics (id, title, announcementid, vendor, vendorcode, authorid, created) 
VALUES(@Id, @Title, @AnnouncementId, @Vendor, @VendorCode, @AuthorId, @Created); 
SELECT LAST_INSERT_ID()";
            long?id       = (await _dbConn.QueryAsync <long>(sqlQuery, item)).FirstOrDefault();

            item.Id = id.Value;
            return(item);
        }
Exemplo n.º 4
0
 internal static Topic MapDbTopic(DbTopic dbTopic)
 {
     if (dbTopic == null)
     {
         return(null);
     }
     return(new Topic()
     {
         Id = dbTopic.Id,
         Name = dbTopic.Name,
     });
 }
Exemplo n.º 5
0
 public void SeedTopics()
 {
     if (!dbcontext.Topics.Any())
     {
         var topic = new DbTopic()
         {
             Name     = "Other",
             Ancestor = null,
         };
         dbcontext.Topics.Add(topic);
         dbcontext.SaveChanges();
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Обновление элемента
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public async Task Update(DbTopic user)
 {
     const string sqlQuery = @"UPDATE dbtopics
                                 SET
                                 title = @Title,
                                 announcementid = @AnnouncementId,
                                 vendor = @Vendor,
                                 vendorcode = @VendorCode,
                                 authorid = @AuthorId,
                                 created = @Created,
                                 updated = @Updated
                                 WHERE id = @Id";
     await _dbConn.ExecuteAsync(sqlQuery, user);
 }
Exemplo n.º 7
0
        internal static TopicDetailed MapDbTopicDetailed(DbTopic dbTopic)
        {
            if (dbTopic == null)
            {
                return(null);
            }
            var ancestor = MapDbTopic(dbTopic.Ancestor);

            return(new TopicDetailed()
            {
                Id = dbTopic.Id,
                Name = dbTopic.Name,
                Ancestor = ancestor,
            });
        }
Exemplo n.º 8
0
        public async Task <Topic> Store(Topic topic, int ancestorId)
        {
            var ancestor = await dbcontext.Topics.FirstOrDefaultAsync(t => t.Id == ancestorId);

            var dbTopic = new DbTopic()
            {
                Name     = topic.Name,
                Ancestor = ancestor,
            };
            var res = await dbcontext.Topics.AddAsync(dbTopic);

            await dbcontext.SaveChangesAsync();

            return(DbMapper.MapDbTopic(res.Entity));
        }
Exemplo n.º 9
0
        public async Task <Topic> Store(TopicDetailed topic)
        {
            DbTopic ancTopic = (topic.Ancestor == null)?null: await dbcontext.Topics.SingleOrDefaultAsync(t => t.Id == topic.Ancestor.Id);

            var dbTopic = new DbTopic()
            {
                Name     = topic.Name,
                Ancestor = ancTopic,
            };
            var res = await dbcontext.Topics.AddAsync(dbTopic);

            await dbcontext.SaveChangesAsync();

            return(DbMapper.MapDbTopic(res.Entity));
        }
Exemplo n.º 10
0
        public async Task <bool> AddTopic(TopicUpsertion topicUpsertion, string loggedUser)
        {
            try
            {
                var currentUser = _userDbContext.NotesUsers.Where(z => z.Email == loggedUser)
                                  .Include(x => x.Topics).SingleOrDefault();

                if (currentUser.Topics == null)
                {
                    currentUser.Topics = new List <DbTopic>();
                }

                var existingTopicCount = currentUser.Topics.Where(x => x.Name == topicUpsertion.Name).Count();

                if (existingTopicCount == 0)
                {
                    DbTopic dbTopic = new DbTopic();
                    dbTopic.Name = topicUpsertion.Name;
                    dbTopic.Tag  = topicUpsertion.Tag;

                    if (dbTopic.Cards == null)
                    {
                        dbTopic.Cards = new List <DbCard>();
                    }

                    foreach (Card card in topicUpsertion.Cards)
                    {
                        DbCard dbCard = new DbCard();
                        dbCard.Content = card.Content;
                        dbCard.Index   = card.Index;

                        dbTopic.Cards.Add(dbCard);
                    }
                    currentUser.Topics.Add(dbTopic);
                    await _userDbContext.SaveChangesAsync();

                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Заполняем поля последнего сообщения в топике, признак прочтения и флаг автор или нет
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private async Task <DbTopic> FillTopicLastMessage(DbTopic topic, int userId)
        {
            var msg = await _ctx.Messages.GetLastMessageForTopic(topic.Id);

            if (msg != null)
            {
                var msgUser = await GetCachedUser(msg.AuthorId);

                topic.LmAuthorId  = msg.AuthorId;
                topic.LmIsCurrent = msg.AuthorId == userId;
                topic.LastMessage = msg.Body;
                topic.LmIsReaded  = msg.IsRead;
                topic.LmCreated   = msg.Created;
                if (msgUser != null)
                {
                    topic.LmName = msgUser.FirstName;
                }
            }

            return(topic);
        }