Exemple #1
0
        public async Task <SocietyModel> SaveSociety(SocietyModel society)
        {
            if (isMockEnabled)
            {
                string json = System.IO.File.ReadAllText(pathFileMockup);
                List <SocietyModel> societies = JsonConvert.DeserializeObject <SocietyCollection>(json).Societies;
                var societyFound = await GetSocietyByName(society.Name);

                if (societyFound == null)
                {
                    society.Id = ObjectId.GenerateNewId().ToString();
                    societies.Add(society);
                }
                else if (societyFound.UserId == society.UserId)
                {
                    societies.Remove(societyFound);
                    society.Id = societyFound.Id;
                    societies.Add(society);
                }

                SocietyCollection collection = new SocietyCollection();
                collection.Societies = societies;
                string jsonUpdate = JsonConvert.SerializeObject(collection);
                //write string to file
                System.IO.File.WriteAllText(pathFileMockup, jsonUpdate);
            }
            else
            {
                try
                {
                    var collection   = _mongoDBContex.GetCollection <SocietyModel>(collectionName);
                    var societyFound = await GetSocietyByName(society.Name);

                    if (societyFound == null)
                    {
                        society.Id = ObjectId.GenerateNewId().ToString();
                        await collection.InsertOneAsync(society);
                    }
                    else if (society.UserId == societyFound.UserId)
                    {
                        society.Id = societyFound.Id;
                        var optionsAndReplace = new FindOneAndReplaceOptions <SocietyModel>
                        {
                            ReturnDocument = ReturnDocument.After
                        };
                        var up = await collection.FindOneAndReplaceAsync <SocietyModel>(u => u.Id == society.Id, society, optionsAndReplace);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return(society);
        }
Exemple #2
0
        public async Task <IActionResult> DeleteSociety(SocietyModel society)
        {
            ResponseContent message = new ResponseContent();
            IActionResult   result  = BadRequest();

            if (isMockEnabled)
            {
                string json = System.IO.File.ReadAllText(pathFileMockup);
                List <SocietyModel> societies = JsonConvert.DeserializeObject <SocietyCollection>(json).Societies;
                var societyFound = await GetSocietyByName(society.Name);

                if (societyFound != null)
                {
                    societies.Remove(societyFound);
                    message.Message = "1 row deleted";
                    result          = Ok(message);
                }

                SocietyCollection collection = new SocietyCollection();
                collection.Societies = societies;
                string jsonUpdate = JsonConvert.SerializeObject(collection);
                //write string to file
                System.IO.File.WriteAllText(pathFileMockup, jsonUpdate);
            }
            else
            {
                try
                {
                    var collection   = _mongoDBContex.GetCollection <SocietyModel>(collectionName);
                    var societyFound = await GetSocietyByName(society.Name);

                    if (societyFound != null)
                    {
                        var deleteFilter = Builders <SocietyModel> .Filter.Where(f => f.Id == society.Id);

                        var count = await collection.DeleteOneAsync(deleteFilter);

                        message.Message = count.DeletedCount.ToString() + " row deleted";
                        result          = Ok(message);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return(result);
        }