Exemple #1
0
        public async void InsertLog(LogMsg logMsg)
        {
            var builder = Builders<User>.Filter;
            var userFilter = builder.Eq("UserId", logMsg.UserId) & builder.Eq("Password", logMsg.Password);
            var userCount = await Users.Find<User>(userFilter).CountAsync();
            User user = logMsg.ToUser();
            Data data = logMsg.ToData();

            if(userCount == 0)
            {
                await Users.InsertOneAsync(user);
                data.UserId = user.Id;
                await Datas.InsertOneAsync(data);
            }
            else
            {
                user = await Users.Find<User>(userFilter).FirstAsync();
                var dataFilter = Builders<Data>.Filter.Eq("Name", logMsg.Name) & Builders<Data>.Filter.Eq("UserId", user.Id);
                var dataCount = await Datas.Find<Data>(dataFilter).CountAsync();
                if(dataCount == 0)
                {
                    var userUpdate = Builders<User>.Update.Push("OwnDataNames", logMsg.Name);
                    await Users.UpdateOneAsync(userFilter, userUpdate);
                    data.UserId = user.Id;
                    await Datas.InsertOneAsync(data);
                }
                else
                {
                    var dataUpdate = Builders<Data>.Update.Push("Logs", logMsg.ToLog());
                    await Datas.UpdateOneAsync(dataFilter, dataUpdate);
                }
            }
        }