Skip to content

Commit

Permalink
Photos can be set as primary from front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Jan 20, 2019
1 parent 6605774 commit e1c1bb9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<div class="col-sm-2" *ngFor="let photo of photos">
<img src="{{photo.url}}" class="img-thumbnail p-1" alt="" />
<div class="text-center">
<button type="button" class="btn btn-xs btn-primary">Main</button>
<button type="button"
class="btn btn-xs btn-primary mr-1"
(click)="setMainPhoto(photo)"
[ngClass]="photo.isMain ? 'btn-success active' : 'btn-secondary'"
[disabled]="photo.isMain">Main</button>

<button type="button" class="btn btn-xs btn-danger"><i class="fa fa-trash-o"></i></button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { FileUploader } from 'ng2-file-upload';
import { Photo } from 'src/app/models/photo';
import { environment } from 'src/environments/environment';
import { AuthService } from 'src/app/services/auth.service';
import { UserService } from 'src/app/services/user.service';
import { AlertifyService } from 'src/app/services/alertify.service';


@Component({
Expand All @@ -17,7 +19,7 @@ export class PhotoEditorComponent implements OnInit {
hasBaseDropZoneOver: boolean = false;
private usersUrl = '/api/users/';

constructor(private authService: AuthService) { }
constructor(private authService: AuthService, private userService: UserService, private alertify: AlertifyService) { }

ngOnInit() {
this.initializeUploader();
Expand Down Expand Up @@ -56,4 +58,14 @@ export class PhotoEditorComponent implements OnInit {
}
}

setMainPhoto(photo: Photo) {
const userId = this.authService.decodedToken.nameid;
this.userService.setMainPhoto(userId, photo.id)
.subscribe(() => {
console.log("Photo set to main");
}, error => {
this.alertify.error(error);
})
}

}
4 changes: 4 additions & 0 deletions Client/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ export class UserService {
updateUser(id: number, user: User) {
return this.http.put(`${this.usersUrl}${id}`, user);
}

setMainPhoto(userId: number, photoId: number) {
return this.http.post(`${this.usersUrl}${userId}/photos/${photoId}/setMain`, {});
}
}
2 changes: 1 addition & 1 deletion Controllers/PhotosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public async Task<ActionResult> SetMainPhoto(int userId, int photoId)
}

var mainPhoto = await _repo.GetMainPhoto(userId);
if (mainPhoto == null)
if (mainPhoto != null)
{
mainPhoto.IsMain = false;
}
Expand Down
2 changes: 1 addition & 1 deletion Data/FriendRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<Photo> GetPhoto(int photoId)

public async Task<Photo> GetMainPhoto(int userId)
{
return await _context.Photos.Where(x => x.Id == userId)
return await _context.Photos.Where(x => x.UserId == userId)
.FirstOrDefaultAsync(x => x.IsMain);
}

Expand Down

0 comments on commit e1c1bb9

Please sign in to comment.