Пример #1
0
        // PUT api/<controller>/5
        public EditMemberResponse Put(EditMemberRequest request)
        {
            var               response         = new EditMemberResponse();
            MongoClient       client           = new MongoClient("mongodb://localhost:27017");
            MongoDatabaseBase db               = client.GetDatabase("ntut") as MongoDatabaseBase;
            var               memberCollection = db.GetCollection <MemberCollection>("members");
            var               query            = Builders <MemberCollection> .Filter.Eq(e => e.uid, request.uid);

            var doc = memberCollection.Find(query).ToListAsync().Result.FirstOrDefault();

            if (doc != null)
            {
                var update = Builders <MemberCollection> .Update.Set("name", request.name).Set("phone", request.phone);

                memberCollection.UpdateOne(query, update);
                //memberCollection.InsertOne(new MemberCollection()
                //{
                //    _id = ObjectId.GenerateNewId(),
                //    uid = request.uid,
                //    name = request.name,
                //    phone = request.phone
                //});
            }
            else
            {
                response.ok     = false;
                response.errMsg = $"Number: {request.uid} is update fail";
            }
            return(response);
        }
Пример #2
0
        /// <summary>
        /// 編輯會員
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public EditMemberResponse Put(EditMemberRequest request)
        {
            var response = new EditMemberResponse();

            MongoClient       client = new MongoClient("mongodb://localhost:27017");
            MongoDatabaseBase db     = (MongoDatabaseBase)client.GetDatabase("ntut");
            var membersCollection    = db.GetCollection <MembersCollection>("members");

            var query = Builders <MembersCollection> .Filter.Eq(e => e.uid, request.uid);

            var doc = membersCollection.Find(query).ToListAsync().Result.FirstOrDefault();

            if (doc != null)
            {
                var update = Builders <MembersCollection> .Update
                             .Set("name", request.name)
                             .Set("phone", request.phone);

                membersCollection.UpdateOne(query, update);
            }
            else
            {
                response.ok     = false;
                response.errMsg = "編號:" + request.uid + "的會員不存在。";
            }

            return(response);
        }
Пример #3
0
        public EditMemberResponse Put(EditMemberRequest request)
        {
            /* 宣告指令的輸出結果 */
            var response = new EditMemberResponse();

            /* Step1 連接MongoDB伺服器 */
            MongoClient client = new MongoClient("mongodb://localhost:27017");

            /* Step2 取得MongoDB資料庫(Database)和集合(Collection) */
            /*   Step2-1 取得ntut資料庫(Database) */
            MongoDatabaseBase db = client.GetDatabase("ntut") as MongoDatabaseBase;
            /*   Step2-2 取得members集合(Collection) */
            var membersCollection = db.GetCollection <MembersCollection>("members");

            /* Step3 修改會員資訊 */
            /*   Step3-1 設定查詢式 */
            var query = Builders <MembersCollection> .Filter.Eq(e => e.uid, request.uid);

            /*   Step3-2 進行查詢的操作,並取得會員資訊 */
            var doc = membersCollection.Find(query).ToListAsync().Result.FirstOrDefault();

            if (doc != null)
            {
                /* Step3-3-1 當資料庫中存在該會員時,進行修改會員資訊的操作 */
                var update = Builders <MembersCollection> .Update
                             .Set("name", request.name)
                             .Set("phone", request.phone);

                membersCollection.UpdateOne(query, update);
            }
            else
            {
                /* Step3-3-2 當資料庫中沒有該會員時,設定Response的ok欄位與errMsg欄位 */
                response.ok     = false;
                response.errMsg = "編號為" + request.uid + "的會員不存在,請確認會員編號。";
            }

            return(response);
        }
Пример #4
0
        public EditMemberResponse Put(EditMemberRequest request)
        {
            var               response          = new EditMemberResponse();
            MongoClient       client            = new MongoClient("mongodb://localhost:27017");
            MongoDatabaseBase db                = client.GetDatabase("ntut") as MongoDatabaseBase;
            var               memberCollection  = db.GetCollection <MembersCollection>("members");
            var               listOfWriteModels = new List <WriteModel <MembersCollection> >();
            var               uids              = request.members.Select(e => e.uid).ToList();
            var               existQuery        = Builders <MembersCollection> .Filter.In(e => e.Uid, uids);

            var doc = memberCollection.Find(existQuery).ToList();

            if (doc.Count > 0)
            {
                var existUIDList = doc.Select(e => e.Uid).ToList();
                //移除不存在在資料庫的會員ID
                var notExistList = request.members.Where(e => !existUIDList.Contains(e.uid)).Select(e => e.uid).ToList();
                var existUid     = string.Join(",", notExistList);
                response.ok     = false;
                response.errMsg = "編號為" + existUid + "的會員不存在在資料庫,請重新確認這些會員編號。";
                request.members.RemoveAll(e => !existUIDList.Contains(e.uid));
            }

            foreach (var member in request.members)
            {
                var query = Builders <MembersCollection> .Filter.Eq(e => e.Uid, member.uid);

                var update = Builders <MembersCollection> .Update
                             .Set(e => e.Name, member.name)
                             .Set(e => e.Phone, member.phone);

                var updateOneModel = new UpdateOneModel <MembersCollection>(query, update);
                listOfWriteModels.Add(updateOneModel);
            }
            memberCollection.BulkWriteAsync(listOfWriteModels);

            return(response);
        }