public static Share ToAppModel(ServiceData.Models.Share given, bool includeOwner) { if (given == null) { return(null); } Share appShare = new Share { Id = given.Id, CreatedAt = given.CreatedAt, ExpireDate = given.ExpireDate, SharedEmail = given.SharedEmail, Updated = given.Updated }; if (given.Owner != null && includeOwner) { appShare.Owner = User.ToAppModel(given.Owner); } if (given.UserCondition != null) { appShare.UserCondition = UserCondition.ToAppModel(given.UserCondition, true); } return(appShare); }
public async Task <HttpResponseMessage> Delete(int id) { ServiceData.Models.Share found = _shareRepository.GetById(id); if (found == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (found.Owner.Email != User.Identity.Name) { return(Request.CreateResponse(HttpStatusCode.Forbidden)); } await _shareRepository.Delete(id); ServerUtils.LogTelemetryEvent(User.Identity.Name, "DeleteShare"); return(Request.CreateResponse(HttpStatusCode.OK)); }
// GET: Conditions public async Task <ActionResult> Index(int id) { await LoadViewBag(); IReadWriteRepository <ServiceData.Models.UserCondition> _condRepository = new UserConditionsRepository(); ServiceData.Models.UserCondition found = _condRepository.GetById(id); if (found == null) { return(new HttpNotFoundResult()); } IReadWriteRepository <ServiceData.Models.Share> _shareRepository = new ShareRepository(); ServiceData.Models.Share sh = _shareRepository.Search(s => s.UserCondition.Id == id && s.SharedEmail == User.Identity.Name && s.ExpireDate > DateTime.UtcNow).FirstOrDefault(); if (found.Owner.Email != User.Identity.Name && sh == null) { return(new HttpUnauthorizedResult()); } // Has been shared with the user (potentially themself but meh) if (sh != null) { sh.Updated = false; _shareRepository.Update(sh); ViewData["Title"] = string.Format("{0}'s {1}", found.Owner.Name, found.Condition); } else { ViewData["Title"] = found.Condition; } Models.UserCondition cond = Models.UserCondition.ToAppModel(found, true); ViewData["Condition"] = cond; return(View(cond.Photos)); }
public async Task <HttpResponseMessage> Post([FromBody] Models.Share newShare) { try { // Does this share already exist? Change the existing share rather than making a new one ServiceData.Models.Share found = _shareRepository.Search(sh => sh.UserCondition.Id == newShare.UserCondition.Id && sh.SharedEmail == newShare.SharedEmail).FirstOrDefault(); Models.Share toRet = null; if (found != null) { found.ExpireDate = newShare.ExpireDate; _shareRepository.Update(found); toRet = Models.Share.ToAppModel(found, false); } else { newShare.CreatedAt = DateTime.Now; newShare.Updated = true; ServiceData.Models.Share returned = _shareRepository.Insert(Models.Share.ToServiceModel(newShare, true)); toRet = Models.Share.ToAppModel(returned, false); await ServerUtils.SendEmail( toRet.SharedEmail, "", "New MySkinSelfie share from " + newShare.UserCondition.Owner.Name, string.Format("{0} has shared their album '{1}' with you." + " Create or log into an account with this email address at {2} to see it!", newShare.UserCondition.Owner.Name, newShare.UserCondition.Condition, string.Format("{0}Conditions/Index/{1}", ConfidentialData.SiteUrl, toRet.UserCondition.Id))); } ServerUtils.LogTelemetryEvent(User.Identity.Name, "AddShare"); return(Request.CreateResponse(HttpStatusCode.OK, toRet)); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, e)); } }