예제 #1
0
        public string DeleteRange(int[] arrId)
        {
            BaseResult baseResult = new BaseResult();

            if (_dataBaseService.DeleteRange(arrId, User.Identity.Name))
            {
                baseResult.Code = 0;
                baseResult.Msg  = "删除成功!";
            }
            else
            {
                baseResult.Code = 1;
                baseResult.Msg  = "删除失败!";
            }
            return(JsonHelper.ObjectToJSON(baseResult));
        }
예제 #2
0
        public async Task UpdateContact(Guid contactId, [FromBody] UpdateContactRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            await using var transaction = await _dbService.Transaction();

            var contactDb = await _dbService.Get <ContactDb>(i => i.Id == contactId)
                            .Include(i => i.ContactsGroups)
                            .Include(i => i.AddressCategories)
                            .Include(i => i.Addresses)
                            .Include(i => i.PhoneCategories)
                            .Include(i => i.Phones)
                            .FirstOrDefaultAsync();

            if (contactDb == null)
            {
                throw new Exception("Контакта не существует");
            }

            var contactsGroups = request.GroupIds?
                                 .Select(i => new ContactGroupDb {
                GroupId = i, ContactId = contactDb.Id
            }).ToList();

            var addressCategories = _mapper.Map <List <AddressCategoryDb> >(request.AddressCategories);
            var phoneCategories   = _mapper.Map <List <PhoneCategoryDb> >(request.PhoneCategories);

            addressCategories.ForEach(i =>
            {
                i.ContactId = contactId;
                foreach (var address in i.Addresses)
                {
                    address.ContactId = contactId;
                }
            });

            phoneCategories.ForEach(i =>
            {
                i.ContactId = contactId;
                foreach (var phone in i.Phones)
                {
                    phone.ContactId = contactId;
                }
            });

            await _dbService.DeleteRange(contactDb.ContactsGroups);

            await _dbService.DeleteRange(contactDb.Addresses);

            await _dbService.DeleteRange(contactDb.Phones);

            await _dbService.DeleteRange(contactDb.AddressCategories);

            await _dbService.DeleteRange(contactDb.PhoneCategories);

            contactDb.Name              = request.Name;
            contactDb.DateOfBirth       = request.DateOfBirth;
            contactDb.Email             = request.Email;
            contactDb.Gender            = request.Gender;
            contactDb.Notes             = JsonSerializer.Serialize(request.Notes);
            contactDb.AddressCategories = addressCategories;
            contactDb.PhoneCategories   = phoneCategories;
            contactDb.ContactsGroups    = contactsGroups;

            await _dbService.Update(contactDb);

            await transaction.CommitAsync();
        }