Esempio n. 1
0
        public IActionResult Delete(int id)
        {
            var returnResult = new ContributionTypeActionResult(false, new List <string>(), null);

            try
            {
                using (var db = new BTAContext())
                {
                    if (db.ContributionType.Any(x => x.ContributionTypeId == id) == false)
                    {
                        returnResult.Success = false;
                        returnResult.StatusMessages.Add(string.Format("Unable to locate contribution type for id: {0}", id));
                        returnResult.Data = null;
                    }
                    else
                    {
                        var resultContributionType = db.ContributionType.Single(x => x.ContributionTypeId == id);
                        db.Remove(resultContributionType);
                        db.SaveChanges();
                        returnResult.Success = true;
                        returnResult.Data    = resultContributionType;
                        returnResult.StatusMessages.Add("Successfully deleted contribution type.");
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString());
                returnResult.Success = false;
                returnResult.StatusMessages.Add("An exception occurred while attempting to delete the contribution type.");
                returnResult.Data = null;
            }
            return(StatusCode(StatusCodes.Status200OK, returnResult));
        }
Esempio n. 2
0
        public IActionResult Put([FromBody] ContributionType contributionType)
        {
            var returnResult = new ContributionTypeActionResult(false, new List <string>(), null);

            if (contributionType != null)
            {
                try
                {
                    using (var db = new BTAContext())
                    {
                        var resultContributionType = db.ContributionType.SingleOrDefault(x => x.ContributionTypeId == contributionType.ContributionTypeId);
                        if (resultContributionType != null)
                        {
                            resultContributionType.CategoryId           = contributionType.CategoryId;
                            resultContributionType.ContributionTypeName = contributionType.ContributionTypeName;
                            resultContributionType.Description          = contributionType.Description;
                            db.SaveChanges();
                            returnResult.Success = true;
                            returnResult.Data    = resultContributionType;
                            returnResult.StatusMessages.Add("Successfully updated contribution type.");
                        }
                        else
                        {
                            returnResult.Success = false;
                            returnResult.StatusMessages.Add(string.Format("Unable to locate contribution type for id: {0}", contributionType.ContributionTypeId));
                            returnResult.Data = null;
                        }
                    }
                }
                catch (Exception e)
                {
                    returnResult.Success = false;
                    returnResult.StatusMessages.Add(e.Message);
                    returnResult.Data = null;
                }
            }
            else
            {
                returnResult.Success = false;
                returnResult.StatusMessages.Add("Empty contribution type posted for update.");
                returnResult.Data = null;
            }
            return(StatusCode(StatusCodes.Status200OK, returnResult));
        }
Esempio n. 3
0
        public IActionResult Post([FromBody] ContributionType contributionType)
        {
            var returnResult = new ContributionTypeActionResult(false, new List <string>(), null);

            if (contributionType != null)
            {
                try
                {
                    using (var db = new BTAContext())
                    {
                        var resultContributionType = db.ContributionType.Add(contributionType);
                        db.SaveChanges();
                        var entity = resultContributionType.Entity;
                        if (entity != null)
                        {
                            returnResult.Success = true;
                            returnResult.StatusMessages.Add("Successfully added contribution type.");
                            returnResult.Data = entity;
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError(e, "An exception occurred while attempting to add a contribution type.");
                    returnResult.Success = false;
                    returnResult.StatusMessages.Add("An exception occurred while attempting to add a contribution type.");
                    returnResult.Data = null;
                }
            }
            else
            {
                returnResult.Success = false;
                returnResult.StatusMessages.Add("Empty contribution type posted for add.");
                returnResult.Data = null;
            }
            return(StatusCode(StatusCodes.Status200OK, returnResult));
        }