Example #1
0
        public bool AddCompetitor(int id, CompetitorCreateApiModel apiModel)
        {
            var validator = _competitorValidator.Validate(apiModel);

            if (validator.IsValid)
            {
                return(_dealRepository.AddCompetitor(id, apiModel));
            }
            return(false);
        }
Example #2
0
        public bool AddCompetitor(int id, CompetitorCreateApiModel apiModel)
        {
            var dbDeal = db.DEALs.Find(id);

            if (dbDeal != null && apiModel != null)
            {
                var dbCompetitor = db.COMPETITORs.Find(apiModel.id);
                if (dbCompetitor != null)
                {
                    var dealCompetitor = dbDeal.DEAL_COMPETITOR.Where(c => c.COMPETITOR_ID == dbCompetitor.ID).FirstOrDefault();
                    dbCompetitor.Website    = apiModel.website;
                    dbCompetitor.Strengths  = apiModel.strengths;
                    dbCompetitor.Weaknesses = apiModel.weaknesses;

                    if (dealCompetitor != null)
                    {
                        dealCompetitor.ThreatLevel = apiModel.threat;
                        dealCompetitor.Suggestions = apiModel.suggestions;
                    }
                    else
                    {
                        var newDealCompetitor = new DEAL_COMPETITOR();
                        newDealCompetitor.DEAL        = dbDeal;
                        newDealCompetitor.COMPETITOR  = dbCompetitor;
                        newDealCompetitor.Suggestions = apiModel.suggestions;
                        newDealCompetitor.ThreatLevel = apiModel.threat;
                        db.DEAL_COMPETITOR.Add(newDealCompetitor);
                    }
                    db.SaveChanges();

                    var owner   = db.USERs.Find(dbDeal.DealOwner);
                    var creator = db.USERs.Find(dbDeal.CreatedBy);

                    var notifyModel = new NotificationApiModel();
                    notifyModel.title          = "Competitor added";
                    notifyModel.content        = $"Competitor {dbCompetitor.Name} has been added to deal {dbDeal.Name}.";
                    notifyModel.createdAt      = DateTime.Now;
                    notifyModel.module         = "deals";
                    notifyModel.moduleObjectId = dbDeal.ID;
                    NotificationManager.SendNotification(notifyModel, new List <USER> {
                        owner, creator
                    });
                    return(true);
                }
                else
                {
                    var newCompetitor = new COMPETITOR();
                    newCompetitor.Name       = apiModel.name;
                    newCompetitor.Strengths  = apiModel.strengths;
                    newCompetitor.Weaknesses = apiModel.weaknesses;
                    newCompetitor.Website    = apiModel.website;
                    db.COMPETITORs.Add(newCompetitor);
                    var newDealCompetitor = new DEAL_COMPETITOR();
                    newDealCompetitor.DEAL_ID     = dbDeal.ID;
                    newDealCompetitor.COMPETITOR  = newCompetitor;
                    newDealCompetitor.Suggestions = apiModel.suggestions;
                    newDealCompetitor.ThreatLevel = apiModel.threat;
                    db.DEAL_COMPETITOR.Add(newDealCompetitor);
                    db.SaveChanges();

                    var owner   = db.USERs.Find(dbDeal.DealOwner);
                    var creator = db.USERs.Find(dbDeal.CreatedBy);

                    var notifyModel = new NotificationApiModel();
                    notifyModel.title     = "Competitor added";
                    notifyModel.content   = $"Competitor {newCompetitor.Name} has been added to deal {dbDeal.Name}.";
                    notifyModel.createdAt = DateTime.Now;
                    NotificationManager.SendNotification(notifyModel, new List <USER> {
                        owner, creator
                    });
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }