예제 #1
0
        public async Task <CustomerDetailItem> CreateCustomerAsync([FromBody] CustomerPayloadItem payload)
        {
            var dbItem = new Customer
            {
                Id        = Guid.NewGuid(),
                City      = payload.City,
                CountryId = payload.Country.Id,
                FirstName = payload.FirstName,
                LastName  = payload.LastName,
                Phone     = payload.Phone,
                Street    = payload.Street,
                ZipCode   = payload.ZipCode,
                Created   = DateTime.UtcNow,
            };

            _db.Add(dbItem);

            var entry = _db.Entry(dbItem);

            entry.UpdateAdditionalProperties(payload);

            await _db.SaveChangesAsync();

            return(await GetCustomerDetailItemAsync(dbItem.Id));
        }
예제 #2
0
        public async Task <IActionResult> PutJob([FromRoute] int id, [FromBody] BindJob bindJob)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != bindJob.JobId)
            {
                return(BadRequest());
            }
            var updatedJob = await _context.Jobs
                             .FirstOrDefaultAsync(j => j.JobId == bindJob.JobId);

            if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == updatedJob.RecruiterId.ToString()) ||
                               (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated)))
            {
                return(BadRequest());
            }

            updatedJob.Title      = bindJob.Title;
            updatedJob.Remarks    = bindJob.Remarks;
            updatedJob.BaseSalary = bindJob.BaseSalary;
            updatedJob.Fee        = bindJob.Fee;
            updatedJob.StartDate  = bindJob.StartDate;
            if (bindJob.ClientId > 0 & updatedJob.ClientId != bindJob.ClientId)
            {
                updatedJob.Client = await _context.CrmClients.FirstOrDefaultAsync(cl => cl.ClientId == bindJob.ClientId);
            }
            if (bindJob.JobStatusId > 0)
            {
                updatedJob.JobStatus = await _context.JobStatuses.FirstOrDefaultAsync(js => js.JobStatusId == bindJob.JobStatusId);
            }

            _context.Entry(updatedJob).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!JobExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #3
0
        public async Task <ActionResult> Put(int id, [FromBody] BindRecruiter bindRecruiter)
        {
            try
            {
                if (id != bindRecruiter.RecruiterId)
                {
                    return(BadRequest());
                }

                var updatedRecruiter = await _context.Recruiters.FirstOrDefaultAsync(r => r.RecruiterId == bindRecruiter.RecruiterId);

                updatedRecruiter.Name  = bindRecruiter.Name;
                updatedRecruiter.Email = bindRecruiter.Email;
                updatedRecruiter.Phone = bindRecruiter.Phone;

                _context.Entry(updatedRecruiter).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> PutCandidateStatus([FromRoute] int id, [FromBody] CandidateStatus candidateStatus)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != candidateStatus.CandidateStatusId)
            {
                return(BadRequest());
            }

            _context.Entry(candidateStatus).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CandidateStatusExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #5
0
        public virtual TEntity Update(TEntity obj)
        {
            var entry = Db.Entry(obj);

            DbSet.Attach(obj);
            entry.State = EntityState.Modified;
            return(obj);
        }
예제 #6
0
        public async Task <ActionResult> Put(string userName, [FromBody] RegisterUser changeUser)
        {
            try
            {
                if (userName != changeUser.UserName)
                {
                    return(BadRequest());
                }
                string newClaimValue;
                if (changeUser.AccessClaim == DefinedClaimAccessValues.Normal)
                {
                    newClaimValue = DefinedClaimAccessValues.Normal;
                }
                else if (changeUser.AccessClaim == DefinedClaimAccessValues.Elevated)
                {
                    newClaimValue = DefinedClaimAccessValues.Elevated;
                }
                else
                {
                    return(BadRequest());
                }

                var user = _userManager.Users.Include(u => u.Recruiter).FirstOrDefault(u => u.UserName == changeUser.UserName);

                if (user == null)
                {
                    return(BadRequest());
                }
                var oldEmail = await _userManager.GetEmailAsync(user);

                if (oldEmail != changeUser.Email)
                {
                    var emailChangeToken = await _userManager.GenerateChangeEmailTokenAsync(user, changeUser.Email);

                    await _userManager.ChangeEmailAsync(user, changeUser.Email, emailChangeToken);
                }

                var recruiter = _context.Recruiters.FirstOrDefault(r => r.RecruiterId == user.Recruiter.RecruiterId);
                recruiter.Name  = changeUser.Name;
                recruiter.Email = changeUser.Email;
                recruiter.Phone = changeUser.Phone;
                var claims = await _userManager.GetClaimsAsync(user);

                var oldClaim = claims.FirstOrDefault(claim => claim.Type == DefinedClaimTypes.Access);

                await _userManager.ReplaceClaimAsync(user, oldClaim, new Claim(DefinedClaimTypes.Access, newClaimValue));

                _context.Entry(recruiter).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                var debugEx = ex;
                return(BadRequest());
            }
        }
예제 #7
0
 public ActionResult Edit([Bind(Include = "SectionName,Job")] Section section)
 {
     if (ModelState.IsValid)
     {
         db.Entry(section).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(section));
 }
 public ActionResult Edit([Bind(Include = "ID,Title,Details,Date,IsDone,PersonId")] ToDoModels toDoModels)
 {
     if (ModelState.IsValid)
     {
         db.Entry(toDoModels).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(toDoModels));
 }
예제 #9
0
 public ActionResult Edit([Bind(Include = "EmployeeId,FirstName,LastName,Age,Adress,SectionName")] Employee employee)
 {
     if (ModelState.IsValid)
     {
         db.Entry(employee).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.SectionName = new SelectList(db.Sections, "SectionName", "Job", employee.SectionName);
     return(View(employee));
 }
예제 #10
0
        private void EventToDb(CrmContext crmContext, string message)
        {
            Notify?.Invoke("Добавляем в лог");
            EventLog eventLog = new EventLog()
            {
                Date        = DateTime.Now,
                Description = message,
                Owner       = "Service",
                Table       = "User"
            };

            crmContext.Entry(eventLog).State = EntityState.Added;
            Notify?.Invoke("Добавляем в CrmContext");
            crmContext.EventLogs.Add(eventLog);
        }
        public async Task <ActionResult> Put(int id, [FromBody] BindClient bindClient)
        {
            try
            {
                if (id != bindClient.ClientId)
                {
                    return(BadRequest());
                }

                if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == bindClient.RecruiterId.ToString()) ||
                                   (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated)))
                {
                    return(BadRequest());
                }

                var updatedClient = await _context.CrmClients.FirstOrDefaultAsync(cl => cl.ClientId == bindClient.ClientId);

                updatedClient.ContactPerson   = bindClient.ContactPerson;
                updatedClient.CompanyName     = bindClient.CompanyName;
                updatedClient.Email           = bindClient.Email;
                updatedClient.TelephoneOffice = bindClient.TelephoneOffice;
                updatedClient.TelephoneMobile = bindClient.TelephoneMobile;
                updatedClient.Designation     = bindClient.Designation;
                updatedClient.Remarks         = bindClient.Remarks;
                updatedClient.Address         = bindClient.Address;

                _context.Entry(updatedClient).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch
            {
                return(BadRequest());
            }
        }
예제 #12
0
 public async Task <ActionResult <Campaign> > UpdateCampaign(int id, [FromBody] Campaign campaign)
 {
     if (campaign == null || id != campaign.Id)
     {
         return(BadRequest());
     }
     _context.Entry(campaign).State = EntityState.Modified;
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         throw;
     }
     return(NoContent());
 }
예제 #13
0
파일: Catalog.cs 프로젝트: vizor111/Crm
        private void btn_update_Click(object sender, System.EventArgs e)
        {
            var id = dataGridView1.SelectedRows[0].Cells[0].Value;

            var entity = _set.Find(id);

            if (entity != null)
            {
                var form = _formFactory.CreateForm(_type, entity);

                form.ShowDialog();

                if (form.DialogResult == DialogResult.OK)
                {
                    _db.Entry(entity).State = EntityState.Modified;
                    _db.SaveChanges();

                    dataGridView1.Update();
                }
            }
        }
예제 #14
0
 public virtual void Update(TEntity obj)
 {
     Db.Entry(obj).State = EntityState.Modified;
 }
예제 #15
0
        public async Task <Models.Crm.Contact> CancelContactChanges(Models.Crm.Contact item)
        {
            var entity = context.Entry(item);

            entity.CurrentValues.SetValues(entity.OriginalValues);
            entity.State = EntityState.Unchanged;

            return(item);
        }
        private async Task ExecuteTrigger(Trigger t)
        {
            string expression = t.GetExpression();

            object[] p       = t.GetParams();
            Type     objType = Type.GetType("Crm." + t.Table);

            if (t.Table.ToLower() == "contact")
            {
                foreach (var action in t.Actions)
                {
                    var contacts = _crmContext.Contact.Where(expression, p).ToList();
                    // Type contactType = contact.GetType();
                    if (action.Type.ToLower() == "update")
                    {
                        if (contacts != null)
                        {
                            foreach (var contact in contacts)
                            {
                                if (action.MetaData == null)
                                {
                                    continue;
                                }

                                string[] values = action.Value.Split("_");
                                string[] fields = action.MetaData.Field.Split("_");
                                if (values.Length != fields.Length)
                                {
                                    throw new Exception(nameof(action));
                                }
                                int count = 0;
                                foreach (var field in fields)
                                {
                                    foreach (var prop in contact.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
                                    {
                                        if (prop.Name.ToLower() == field.ToLower())
                                        {
                                            // FieldInfo fieldInfo = contactType.GetFieldInfo(prop.Name);
                                            // fieldInfo.SetValue(null, action.Value);
                                            var propType   = prop.PropertyType;
                                            var targetType = propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)) ? Nullable.GetUnderlyingType(propType) : propType;
                                            try
                                            {
                                                var new_val = Convert.ChangeType(values[count], targetType, null);
                                                prop.SetValue(contact, new_val);
                                            }
                                            catch (InvalidCastException e)
                                            {
                                                throw new InvalidCastException(e.Message);
                                            }
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    count++;
                                }

                                _crmContext.Entry(contact).State = EntityState.Modified;
                            }
                        }
                    }
                    else if (action.Type.ToLower() == "delete")
                    {
                        if (contacts != null)
                        {
                            //TODO delete action
                            foreach (var contact in contacts)
                            {
                                _crmContext.Contact.Remove(contact);
                            }
                        }
                    }
                    else if (action.Type.ToLower() == "create")
                    {
                        Contact template = new Contact();
                        //TODO create action
                        if (action.MetaData == null)
                        {
                            continue;
                        }

                        string[] values = action.Value.Split("_");
                        string[] fields = action.MetaData.Field.Split("_");
                        if (values.Length != fields.Length)
                        {
                            throw new Exception(nameof(action));
                        }
                        int count = 0;
                        foreach (var field in fields)
                        {
                            foreach (var prop in template.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
                            {
                                if (prop.Name == field)
                                {
                                    // FieldInfo fieldInfo = contactType.GetFieldInfo(prop.Name);
                                    // fieldInfo.SetValue(null, action.Value);
                                    prop.SetValue(template, values[count]);
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            count++;
                        }
                        _crmContext.Contact.Add(template);
                    }
                }
                try
                {
                    await _crmContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                //TODO do action depend on prop, val
            }
            else if (t.Table.ToLower() == "campaign")
            {
                foreach (var action in t.Actions)
                {
                    var campaigns = _crmContext.Campaign.Where(expression, p).ToList();
                    // Type campaignType = campaign.GetType();
                    if (action.Type.ToLower() == "update")
                    {
                        if (campaigns != null)
                        {
                            foreach (var campaign in campaigns)
                            {
                                if (action.MetaData == null)
                                {
                                    continue;
                                }

                                string[] values = action.Value.Split("_");
                                string[] fields = action.MetaData.Field.Split("_");
                                if (values.Length != fields.Length)
                                {
                                    throw new Exception(nameof(action));
                                }
                                int count = 0;
                                foreach (var field in fields)
                                {
                                    foreach (var prop in campaign.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
                                    {
                                        if (prop.Name.ToLower() == field.ToLower())
                                        {
                                            // FieldInfo fieldInfo = campaignType.GetFieldInfo(prop.Name);
                                            // fieldInfo.SetValue(null, action.Value);
                                            var propType   = prop.PropertyType;
                                            var targetType = propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)) ? Nullable.GetUnderlyingType(propType) : propType;
                                            try
                                            {
                                                var new_val = Convert.ChangeType(values[count], targetType, null);
                                                prop.SetValue(campaign, new_val);
                                            }
                                            catch (InvalidCastException e)
                                            {
                                                throw new InvalidCastException(e.Message);
                                            }
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    count++;
                                }

                                _crmContext.Entry(campaign).State = EntityState.Modified;
                            }
                        }
                    }
                    else if (action.Type.ToLower() == "delete")
                    {
                        if (campaigns != null)
                        {
                            //TODO delete action
                            foreach (var campaign in campaigns)
                            {
                                _crmContext.Campaign.Remove(campaign);
                            }
                        }
                    }
                    else if (action.Type.ToLower() == "create")
                    {
                        Campaign template = new Campaign();
                        //TODO create action
                        if (action.MetaData == null)
                        {
                            continue;
                        }

                        string[] values = action.Value.Split("_");
                        string[] fields = action.MetaData.Field.Split("_");
                        if (values.Length != fields.Length)
                        {
                            throw new Exception(nameof(action));
                        }
                        int count = 0;
                        foreach (var field in fields)
                        {
                            foreach (var prop in template.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
                            {
                                if (prop.Name == field)
                                {
                                    prop.SetValue(template, values[count]);
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            count++;
                        }
                        _crmContext.Campaign.Add(template);
                    }
                }
                try
                {
                    await _crmContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                //TODO do action depend on prop, val
            }
        }
        public async Task <IActionResult> PutCandidate([FromRoute] int id, [FromBody] BindCandidate bindCandidate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != bindCandidate.CandidateId)
            {
                return(BadRequest());
            }

            if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == bindCandidate.RecruiterId.ToString()) ||
                               (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated)))
            {
                return(BadRequest());
            }


            var updatedCandidate = await _context.Candidates.FirstOrDefaultAsync(c => c.CandidateId == bindCandidate.CandidateId);

            updatedCandidate.Name  = bindCandidate.Name;
            updatedCandidate.Email = bindCandidate.Email;
            updatedCandidate.Phone = bindCandidate.Phone;
            if (bindCandidate.JobId > 0)
            {
                var candidateSent = new CandidateSentToIntrerview()
                {
                    JobId = bindCandidate.JobId, CandidateId = bindCandidate.CandidateId
                };
                _context.CandidatesSentToInterview.Add(candidateSent);
            }
            if (bindCandidate.RecruiterId > 0)
            {
                updatedCandidate.Recruiter = await _context.Recruiters.FirstOrDefaultAsync(r => r.RecruiterId == bindCandidate.RecruiterId);
            }
            if (bindCandidate.CandidateStatusId > 0)
            {
                updatedCandidate.CandidateStatus = await _context.CandidateStatuses.FirstOrDefaultAsync(s => s.CandidateStatusId == bindCandidate.CandidateStatusId);
            }

            _context.Entry(updatedCandidate).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CandidateExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(BadRequest());
                }
            }catch (Exception ex)
            {
                var debugEx = ex;
                return(BadRequest());
            }

            return(NoContent());
        }
예제 #18
0
        public async Task <Models.Crm.Contact> DeleteContact(int?id)
        {
            var item = context.Contacts
                       .Where(i => i.Id == id)
                       .Include(i => i.Opportunities)
                       .FirstOrDefault();

            if (item == null)
            {
                throw new Exception("Item no longer available");
            }

            OnContactDeleted(item);

            context.Contacts.Remove(item);

            try
            {
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                context.Entry(item).State = EntityState.Unchanged;
                throw ex;
            }

            return(item);
        }