Exemplo n.º 1
0
        public async Task <IHttpActionResult> PutQrProfile(Guid id, QrProfile qrProfile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != qrProfile.Id)
            {
                return(BadRequest());
            }

            db.Entry(qrProfile).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QrProfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> RetrieveProfile(int id)
        {
            QrProfile QrProfile = await db.QrProfiles.FindAsync(id);

            if (QrProfile == null)
            {
                return(NotFound());
            }

            return(Ok(QrProfile));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> PostQrProfile(QrProfile qrProfile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.QrProfiles.Add(qrProfile);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = qrProfile.Id }, qrProfile));
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> DeleteQrProfile(int id)
        {
            QrProfile qrProfile = await db.QrProfiles.FindAsync(id);

            if (qrProfile == null)
            {
                return(NotFound());
            }

            db.QrProfiles.Remove(qrProfile);
            await db.SaveChangesAsync();

            return(Ok(qrProfile));
        }
        public async Task <IHttpActionResult> DeleteQrProfile(int id)
        {
            var       userId    = this.User.Identity.GetUserId();
            QrProfile QrProfile = await db.QrProfiles.FindAsync(id);

            if (QrProfile == null)
            {
                return(NotFound());
            }

            db.QrProfiles.Remove(QrProfile);
            await db.SaveChangesAsync();

            return(Ok(QrProfile));
        }
        public async Task <IHttpActionResult> PostQrProfile(CreateProfileModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var profileToSave = new QrProfile();
            var user          = await CreateUpdateUser(model);

            profileToSave.UserId = user.Id;

            var userId = this.User.Identity.GetUserId();

            profileToSave.Id = Guid.NewGuid();
            profileToSave.CreatedByUserId = userId;
            profileToSave.FirstName       = model.FirstName;
            profileToSave.MiddleName      = model.MiddleName;
            profileToSave.LastName        = model.LastName;
            profileToSave.Sex             = model.Sex;
            profileToSave.Birthday        = model.Birthday.Value;
            profileToSave.DateOfDeath     = model.DateOfDeath.Value;
            //profileToSave.Obituary = model.Obituary;
            db.QrProfiles.Add(profileToSave);
            db.UserToProfileRelationships.Add(new UserToProfileRelationship()
            {
                ProfileId = profileToSave.Id, UserId = user.Id
            });
            if (userId != user.Id)
            {
                db.UserToProfileRelationships.Add(new UserToProfileRelationship()
                {
                    ProfileId = profileToSave.Id, UserId = userId
                });
            }
            //attempt payment before saving profile
            if (!String.IsNullOrEmpty(model.CardToken) || !User.IsInRole("Admin"))
            {
                var message = ChargeCard(model.CardToken);
                if (!String.IsNullOrEmpty(message))
                {
                    return(BadRequest(message));
                }
            }
            await db.SaveChangesAsync();

            var viewQrUrl = Url.Request.RequestUri.Scheme + "://" + Url.Request.RequestUri.Authority + Url.Route("QrManagement", null) + "/" + profileToSave.Id;

            var profileCreatedMessage = $@"
				<html>
					<body>
						<p>A new Embracing Memories profile has been created for {model.FirstName} {model.LastName}</p>
						<br />
						<p><a href=""{viewQrUrl}"">{viewQrUrl}</a></p>
						<br />
						<table>
							<tr>
								<td>{model.UserFirstName} {model.UserLastName}</td>
							</tr><tr>
								<td>{model.UserAddressLine1}</td>
							</tr><tr>
								<td>{model.UserAddressLine2}</td>
							</tr><tr>
								<td>{model.UserCity}, {model.UserState}&nbsp;&nbsp;{model.UserPostalCode}</td>
							</tr>
						</table>
						<br />
						{EmailService.Signature}
					</body>
				</html>
			"            ;
            await EmailService.SendAsync(new EmailService.EmailMessage()
            {
                Body       = profileCreatedMessage,
                Recipients = model.UserEmail,
                Subject    = String.Format("Embracing Memories profile for {0} {1}", model.FirstName, model.LastName)
            });

            return(CreatedAtRoute("ViewProfile", new { id = profileToSave.Id }, profileToSave));
        }
        public async Task <IHttpActionResult> PutQrProfile(Guid id, QrProfile profile)
        {
            var existingParent = db.QrProfiles
                                 .Where(p => p.Id == profile.Id)
                                 .Include(p => p.Links)
                                 .SingleOrDefault();

            if (existingParent != null)
            {
                foreach (var link in profile.Links)
                {
                    link.QrProfileId = profile.Id;
                }
                var userId = this.User.Identity.GetUserId();
                profile.UserId = userId;
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (id != profile.Id)
                {
                    return(BadRequest());
                }

                // Update parent
                db.Entry(existingParent).CurrentValues.SetValues(profile);

                #region Links
                // Delete children
                foreach (var existingChild in existingParent.Links.ToList())
                {
                    if (!profile.Links.Any(c => c.Id == existingChild.Id))
                    {
                        db.Links.Remove(existingChild);
                    }
                }

                // Update and Insert children
                foreach (var childModel in profile.Links)
                {
                    var existingChild = existingParent.Links
                                        .Where(c => c.Id == childModel.Id)
                                        .SingleOrDefault();

                    if (existingChild != null)
                    {
                        // Update child
                        db.Entry(existingChild).CurrentValues.SetValues(childModel);
                    }
                    else
                    {
                        // Insert child
                        var newChild = new QrLink
                        {
                            Id          = childModel.Id,
                            Label       = childModel.Label,
                            QrProfileId = childModel.QrProfileId,
                            Url         = childModel.Url
                        };
                        existingParent.Links.Add(newChild);
                    }
                }
                #endregion

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!QrProfileExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            //var i = 0;
            //foreach (var l in QrProfile.Links)
            //{
            //	l.QrProfileId = QrProfile.Id;
            //	l.Id = i++;
            //}
            //var userId = this.User.Identity.GetUserId();
            //QrProfile.UserId = userId;
            //if (!ModelState.IsValid)
            //{
            //	return BadRequest(ModelState);
            //}

            //if (id != QrProfile.Id)
            //{
            //	return BadRequest();
            //}

            //db.Entry(QrProfile).State = EntityState.Modified;

            //try
            //{
            //	await db.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //	if (!QrProfileExists(id))
            //	{
            //		return NotFound();
            //	}
            //	else
            //	{
            //		throw;
            //	}
            //}
            return(Ok(existingParent));
        }
Exemplo n.º 8
0
        public async Task <IHttpActionResult> GetQrProfile(int id)
        {
            QrProfile qrProfile = await db.QrProfiles.FindAsync(id);

            return(Ok(qrProfile));
        }