public AgentView AddAgent(AgentView agent)
 {
     try
     {
         return _agentService.SaveAgent(agent);
     }
     catch (Exception ex)
     {
         throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
     }
 }
예제 #2
0
        /// <summary>
        /// Create or update agent
        /// </summary>
        /// <param name="agentView"></param>
        /// <returns></returns>
        public AgentView SaveAgent(AgentView agentView)
        {
            using (var context = new GleanerDbContext()) {
                context.Database.Log = s => Debug.WriteLine(s);

                var isNew = String.IsNullOrEmpty(agentView.AgentNumber);
                if (isNew) {
                    throw new Exception("You can not create an agent!");
                }

                // get agent system data
                var agentSystem = isNew ? new AgentSystem() : GetAgentSystemByKey(context, agentView.AgentNumber);

                /*
                // get agency
                var agencies = context.Agencies.Where(x => x.AgcAgencyName == agentView.AgencyName);
                if (!agencies.Any()) throw new Exception("Could not find an agency with name '" + agentView.AgencyName + "'.");
                if (agencies.Count() > 2) throw new Exception("There are several agencies with the same name.");
                var agencyId = agencies.First().AgcAgencyNumber;

                // create agent record
                var agent = agentView.ToAgent();
                agent.AgcAgencyNumber = agencyId;

                if (isNew)
                {
                    // get new agent number
                    var isSuccess = 0;
                    var newAgentNumberResult = context.GetNewAgentNumber(agencyId, out isSuccess);
                    if (!newAgentNumberResult.Any()) throw new Exception("Could not create new agent number.");
                    var newAgentNumber = newAgentNumberResult.First().Value;

                    agent.AgentNumber = newAgentNumber;

                    agentSystem.AgentNumber = newAgentNumber;
                    agentSystem.IsActivated = false;
                    agentSystem.IsLocked = false;
                    agentSystem.IsDeleted = false;
                    agentSystem.ConfirmKey = String.Format("{0}{1}", Guid.NewGuid(), Guid.NewGuid()).Replace("-", "");
                }
                else
                */
                {
                    agentSystem.IsActivated = agentView.IsActivated;
                    agentSystem.IsLocked = agentView.IsLocked;
                    agentSystem.UpdateDate = DateTime.Now;
                }
                agentSystem.UserName = agentView.UserName;

                // create/update agent
                // context.Agents.AddOrUpdate(agent);
                context.AgentSystems.AddOrUpdate(agentSystem);
                DbHelper.SaveChanges(context);

                /*
                if (isNew)
                {
                    // Send the email for activation
                    umbraco.library.SendMail(_emailSender, agent.EmailAddress,
                        "Gleaner Agent Activation",
                        String.Format("<a href='http://gleaner.local/activation?agent={0}&activationKey={1}' target='_blank'>Activation link</a>", agentSystem.AgentNumber, agentSystem.ConfirmKey),
                        true);
                }
                */

                return GetAgent(agentView.AgentNumber);
            }
        }