Esempio n. 1
0
        public async Task <IHttpActionResult> PostAsync([FromBody] ProjectItemDto model, CancellationToken cancellationToken)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            var user = await _userManager.FindByEmailAsync(User.Identity.Name, cancellationToken);

            if (user == null)
            {
                return(NotFound());
            }
            if (model.Sender == null || model.Sender.Address == null)
            {
                model.Sender = MailAddressItem.Create(ApiSecurity.CurrentUserName);
            }

            var project = new ProjectItem {
                Name = model.Name, Sender = model.Sender
            };
            var validationResult = await _projectManager.CreateAsync(project, new[] { user }, cancellationToken);

            if (!validationResult.Succeeded)
            {
                return(this.ValidationContent(validationResult));
            }

            var contact = new ContactItem {
                Email = model.Sender
            };
            var businessTag = new BusinessTagItem {
                Name = "Feliratkozott", Color = "#00ff00"
            };
            await _projectManager.AddContactAsync(project, contact, cancellationToken);

            await _projectManager.AddBusinessTagAsync(project, businessTag, cancellationToken);

            await _projectManager.SetBusinessTagsAsync(new[] { contact.Id }, new[] { businessTag.Id }, new int[0], cancellationToken);

            return(CreatedAtRoute("Projects.GetById", new RouteValueDictionary {
                { "id", project.Id }
            }, new ProjectResultDto
            {
                Id = project.Id,
                Name = project.Name,
                Sender = project.Sender
            }));
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> PutAsync(int id, [FromBody] ProjectItemDto model, CancellationToken cancellationToken)
        {
            if (model == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }
            var project = new ProjectItem {
                Id = id, Name = model.Name, Sender = model.Sender
            };
            await ApiSecurity.AuthorizeAsync(project, AccessPermission.CanEdit, cancellationToken);

            var validationResult = await _projectManager.UpdateAsync(project, cancellationToken);

            if (!validationResult.Succeeded)
            {
                return(this.ValidationContent(validationResult));
            }
            return(Ok(new ProjectResultDto
            {
                Id = project.Id,
                Name = project.Name,
                Sender = project.Sender
            }));
        }