示例#1
0
 public bool CreateApi(ComparisonCreate data, string username)
 {
     try
     {
         this.Create(data, username);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#2
0
        public void Create(ComparisonCreate data, string username)
        {
            var parentDirId = data.ParentDirId;

            var user = this.context.Users.SingleOrDefault(x => x.UserName == username);

            if (user == null)
            {
                throw new UserNotFound(username);
            }

            var parentDir = this.context.Directories.SingleOrDefault(x => x.Id == parentDirId);

            if (parentDir == null)
            {
                throw new ItemNotFound("The parent directory for the comparison you are trying to create does not exist!");
            }

            ///Check the Dir belongs to the user
            if (parentDir.UserId != user.Id)
            {
                throw new AccessDenied("The directory you are trying to put the comparison in does not belong to you!");
            }

            var numberOfExistingComparisonsInGivenDirectory = this.context.Directories.
                                                              Where(x => x.Id == parentDirId)
                                                              .Select(x => x.Comparisons.Count)
                                                              .SingleOrDefault();
            var order = numberOfExistingComparisonsInGivenDirectory;

            var comparison = new Comparison
            {
                UserId         = user.Id,
                DirectoryId    = parentDir.Id,
                Order          = order,
                Name           = data.Name,
                Description    = data.Description,
                TargetLanguage = data.TargetLanguage,
                SourceLanguage = data.SourceLanguage,
            };

            context.Comparisons.Add(comparison);
            context.SaveChanges();
        }
示例#3
0
        public ActionResult <bool> Create([FromBody] ComparisonCreate data)
        {
            var result = this.comparisonService.CreateApi(data, this.User.Identity.Name);

            return(result);
        }